~7010622-q-deactivatedaccount/widelands/topple-trainingsites-1

« back to all changes in this revision

Viewing changes to src/wui/minimap.cc

  • Committer: Bunnybot
  • Date: 2016-10-24 20:32:29 UTC
  • mfrom: (8131.2.42 zoom)
  • Revision ID: bunnybot@raspberrypi-20161024203229-0q9eolrxn69u0eoq
Merged lp:~widelands-dev/widelands/zoom:
- Adds mouse wheel zoom to the MapView and adapt code for these changes.
  - Overlays (text & soldier icons) are drawn at integer scale only and not at all once scale is < 0.5.
  - This required to change all blitting to be sub-pixel - i.e. float - for the 'destination'. 
- Adapted and simplified minimap rendering. It gained a feature too: Minimaps can be drawn using two modes :
    - the map scrolls and the view window stands still (the old one, stil used for in-game)
    - the view window scrolls and the map always looks like the minimap preview. This is now used in the editor.
- Renames Vector -> Vector3f, Point -> Vector2i, Pointf -> Vector2f, Rect -> Recti, FloatRect -> Rectf.
- Refactored object drawing to be simpler and slightly faster:
    - Never pass the field to be drawn to the draw routine. 
    - Do not calculate if statics or census should be shown inside the draw routines of the objects, but outside.
    - Move 'owner' field to MapObject - it was duplicated on Bob, Immovable and PlayerImmovable.

Known issues that I will not fix in this branch:
Lua is not aware of zoom and uses zoomless calculations for its moving functions of the view. That means that if the player has zoomed in a scenario and the code wants to move to a certain field, the movement will be wrong. My plan is to remove the Lua functions and add a Animator class to the MapView that will do smooth animations over time in the next branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
MiniMap::View::View(UI::Panel& parent,
34
34
                    MiniMapLayer* flags,
 
35
                    MiniMapType* type,
35
36
                    int32_t const x,
36
37
                    int32_t const y,
37
38
                    uint32_t const,
39
40
                    InteractiveBase& ibase)
40
41
   : UI::Panel(&parent, x, y, 10, 10),
41
42
     ibase_(ibase),
42
 
     viewx_(0),
43
 
     viewy_(0),
44
43
     pic_map_spot_(g_gr->images().get("images/wui/overlays/map_spot.png")),
45
 
     flags_(flags) {
 
44
     minimap_layers_(flags),
 
45
     minimap_type_(type) {
46
46
}
47
47
 
48
 
/** MiniMap::View::set_view_pos(int32_t x, int32_t y)
49
 
 *
50
 
 * Set the view point marker to a new position.
51
 
 *
52
 
 * Args: x, y  new view point coordinates, in screen coordinates
53
 
 */
54
 
void MiniMap::View::set_view_pos(const int32_t x, const int32_t y) {
55
 
        viewx_ = x / kTriangleWidth;
56
 
        viewy_ = y / kTriangleHeight;
 
48
void MiniMap::View::set_view(const Rectf& view_area) {
 
49
        view_area_ = view_area;
57
50
}
58
51
 
59
52
void MiniMap::View::draw(RenderTarget& dst) {
60
 
        minimap_image_ = draw_minimap(ibase_.egbase(), ibase_.get_player(),
61
 
                                      (*flags_) & (MiniMapLayer::Zoom2) ?
62
 
                                         Point((viewx_ - get_w() / 4), (viewy_ - get_h() / 4)) :
63
 
                                         Point((viewx_ - get_w() / 2), (viewy_ - get_h() / 2)),
64
 
                                      *flags_ | MiniMapLayer::ViewWindow);
65
 
        dst.blit(Point(), minimap_image_.get());
 
53
        minimap_image_ = draw_minimap(ibase_.egbase(), ibase_.get_player(), view_area_, *minimap_type_,
 
54
                                      *minimap_layers_ | MiniMapLayer::ViewWindow);
 
55
        dst.blit(Vector2f(), minimap_image_.get());
66
56
}
67
57
 
68
58
/*
74
64
        if (btn != SDL_BUTTON_LEFT)
75
65
                return false;
76
66
 
77
 
        //  calculates the coordinates corresponding to the mouse position
78
 
        Widelands::Coords c;
79
 
        if (*flags_ & MiniMapLayer::Zoom2)
80
 
                c = Widelands::Coords(viewx_ + 1 - (get_w() / 2 - x) / 2, viewy_ + 1 - (get_h() / 2 - y) / 2);
81
 
        else
82
 
                c = Widelands::Coords(viewx_ + 1 - get_w() / 2 + x, viewy_ + 1 - get_h() / 2 + y);
83
 
 
84
 
        ibase_.egbase().map().normalize_coords(c);
85
 
 
86
 
        dynamic_cast<MiniMap&>(*get_parent()).warpview(c.x * kTriangleWidth, c.y * kTriangleHeight);
87
 
 
 
67
        dynamic_cast<MiniMap&>(*get_parent())
 
68
           .warpview(minimap_pixel_to_mappixel(ibase_.egbase().map(), Vector2i(x, y), view_area_,
 
69
                                               *minimap_type_, *minimap_layers_ & MiniMapLayer::Zoom2));
88
70
        return true;
89
71
}
 
72
 
90
73
bool MiniMap::View::handle_mouserelease(uint8_t const btn, int32_t, int32_t) {
91
74
        return btn == SDL_BUTTON_LEFT;
92
75
}
129
112
}
130
113
MiniMap::MiniMap(InteractiveBase& ibase, Registry* const registry)
131
114
   : UI::UniqueWindow(&ibase, "minimap", registry, 0, 0, _("Map")),
132
 
     view_(*this, &registry->flags, 0, 0, 0, 0, ibase),
 
115
     view_(*this, &registry->minimap_layers, &registry->minimap_type, 0, 0, 0, 0, ibase),
133
116
 
134
117
     button_terrn(this,
135
118
                  "terrain",
219
202
}
220
203
 
221
204
void MiniMap::toggle(MiniMapLayer const button) {
222
 
        *view_.flags_ = MiniMapLayer(*view_.flags_ ^ button);
 
205
        *view_.minimap_layers_ = MiniMapLayer(*view_.minimap_layers_ ^ button);
223
206
        if (button == MiniMapLayer::Zoom2)
224
207
                resize();
225
208
        update_button_permpressed();
226
209
}
227
210
 
228
211
void MiniMap::resize() {
229
 
        view_.set_zoom(*view_.flags_ & MiniMapLayer::Zoom2 ? 2 : 1);
 
212
        view_.set_zoom(*view_.minimap_layers_ & MiniMapLayer::Zoom2 ? 2 : 1);
230
213
        set_inner_size(view_.get_w(), view_.get_h() + number_of_button_rows() * but_h());
231
 
        button_terrn.set_pos(Point(but_w() * 0, view_.get_h() + but_h() * 0));
 
214
        button_terrn.set_pos(Vector2i(but_w() * 0, view_.get_h() + but_h() * 0));
232
215
        button_terrn.set_size(but_w(), but_h());
233
 
        button_owner.set_pos(Point(but_w() * 1, view_.get_h() + but_h() * 0));
 
216
        button_owner.set_pos(Vector2i(but_w() * 1, view_.get_h() + but_h() * 0));
234
217
        button_owner.set_size(but_w(), but_h());
235
 
        button_flags.set_pos(Point(but_w() * 2, view_.get_h() + but_h() * 0));
 
218
        button_flags.set_pos(Vector2i(but_w() * 2, view_.get_h() + but_h() * 0));
236
219
        button_flags.set_size(but_w(), but_h());
237
 
        button_roads.set_pos(Point(but_w() * 0, view_.get_h() + but_h() * 1));
 
220
        button_roads.set_pos(Vector2i(but_w() * 0, view_.get_h() + but_h() * 1));
238
221
        button_roads.set_size(but_w(), but_h());
239
 
        button_bldns.set_pos(Point(but_w() * 1, view_.get_h() + but_h() * 1));
 
222
        button_bldns.set_pos(Vector2i(but_w() * 1, view_.get_h() + but_h() * 1));
240
223
        button_bldns.set_size(but_w(), but_h());
241
 
        button_zoom.set_pos(Point(but_w() * 2, view_.get_h() + but_h() * 1));
 
224
        button_zoom.set_pos(Vector2i(but_w() * 2, view_.get_h() + but_h() * 1));
242
225
        button_zoom.set_size(but_w(), but_h());
243
226
        move_inside_parent();
244
227
}
245
228
 
246
 
// Makes the buttons reflect the selected layers
247
229
void MiniMap::update_button_permpressed() {
248
 
        button_terrn.set_perm_pressed(*view_.flags_ & MiniMapLayer::Terrain);
249
 
        button_owner.set_perm_pressed(*view_.flags_ & MiniMapLayer::Owner);
250
 
        button_flags.set_perm_pressed(*view_.flags_ & MiniMapLayer::Flag);
251
 
        button_roads.set_perm_pressed(*view_.flags_ & MiniMapLayer::Road);
252
 
        button_bldns.set_perm_pressed(*view_.flags_ & MiniMapLayer::Building);
253
 
        button_zoom.set_perm_pressed(*view_.flags_ & MiniMapLayer::Zoom2);
 
230
        button_terrn.set_perm_pressed(*view_.minimap_layers_ & MiniMapLayer::Terrain);
 
231
        button_owner.set_perm_pressed(*view_.minimap_layers_ & MiniMapLayer::Owner);
 
232
        button_flags.set_perm_pressed(*view_.minimap_layers_ & MiniMapLayer::Flag);
 
233
        button_roads.set_perm_pressed(*view_.minimap_layers_ & MiniMapLayer::Road);
 
234
        button_bldns.set_perm_pressed(*view_.minimap_layers_ & MiniMapLayer::Building);
 
235
        button_zoom.set_perm_pressed(*view_.minimap_layers_ & MiniMapLayer::Zoom2);
254
236
}