Browse Source

Improve minimap

Eduardo Bart 8 years ago
parent
commit
c0c2411854

+ 22
- 24
modules/game_minimap/minimap.lua View File

@@ -4,7 +4,7 @@ Minimap = {}
4 4
 local minimapWidget
5 5
 local minimapButton
6 6
 local minimapWindow
7
-local DEFAULT_ZOOM = 45
7
+local DEFAULT_ZOOM = 60
8 8
 minimapFirstLoad = true
9 9
 
10 10
 -- private functions
@@ -31,15 +31,15 @@ function onMinimapMouseWheel(self, mousePos, direction)
31 31
 end
32 32
 
33 33
 --[[
34
-  Known Issue (TODO): 
35
-  If you move the minimap compass directions and 
34
+  Known Issue (TODO):
35
+  If you move the minimap compass directions and
36 36
   you change floor it will not update the minimap.
37 37
 ]]
38 38
 -- public functions
39 39
 function Minimap.init()
40 40
   connect(g_game, { onGameStart = Minimap.reset,
41 41
                     onForceWalk = Minimap.center })
42
-                    
42
+
43 43
   g_keyboard.bindKeyDown('Ctrl+M', Minimap.toggle)
44 44
 
45 45
   minimapButton = TopMenu.addRightGameToggleButton('minimapButton', tr('Minimap') .. ' (Ctrl+M)', 'minimap.png', Minimap.toggle)
@@ -79,7 +79,7 @@ end
79 79
 function Minimap.terminate()
80 80
   disconnect(g_game, { onGameStart = Minimap.reset,
81 81
                        onForceWalk = Minimap.center })
82
-                       
82
+
83 83
   g_keyboard.unbindKeyDown('Ctrl+M')
84 84
 
85 85
   minimapButton:destroy()
@@ -105,30 +105,28 @@ function Minimap.onMiniWindowClose()
105 105
 end
106 106
 
107 107
 function Minimap.isClickInRange(position, fromPosition, toPosition)
108
-	return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.x <= toPosition.x and position.y <= toPosition.y)
108
+  return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.x <= toPosition.x and position.y <= toPosition.y)
109 109
 end
110
+
110 111
 -- hooked functions
111
-local compassZones = {}
112
-compassZones.west = {x = 0, y = 30, posx = -1, posy = 0}
113
-compassZones.north = {x = 30, y = 0, posx = 0, posy = -1}
114
-compassZones.south = {x = 30, y = 57, posx = 0, posy = 1}
115
-compassZones.east = {x = 57, y = 30, posx = 1, posy = 0}
116 112
 function Minimap.compassClick(self, mousePos)
117
-  local compassPos = self:getRect()
118
-  local pos = {x = mousePos.x-compassPos.x, y = mousePos.y-compassPos.y}
119
-  local move = {x = 0, y = 0}
120
-  for i,v in pairs(compassZones) do
121
-    local lowPos = {x = v.x-15, y = v.y-15}
122
-    local highPos = {x = v.x+15, y = v.y+15}
123
-    if Minimap.isClickInRange(pos, lowPos, highPos) then
124
-      move.x = move.x + v.posx * minimapWidget:getZoom()/10
125
-      move.y = move.y + v.posy * minimapWidget:getZoom()/10
126
-      break
127
-    end
128
-  end
113
+  local px = mousePos.x - self:getX()
114
+  local py = mousePos.y - self:getY()
115
+  local dx = px - self:getWidth()/2
116
+  local dy = -(py - self:getHeight()/2)
117
+  local radius = math.sqrt(dx*dx+dy*dy)
118
+  local movex=0
119
+  local movey=0
120
+  dx = dx/radius
121
+  dy = dy/radius
122
+
123
+  if dx > 0.5 then movex = 1 end
124
+  if dx < -0.5 then movex = -1 end
125
+  if dy > 0.5 then movey = -1 end
126
+  if dy < -0.5 then movey = 1 end
129 127
 
130 128
   local cameraPos = minimapWidget:getCameraPosition()
131
-  local pos = {x = cameraPos.x + move.x, y = cameraPos.y + move.y, z = cameraPos.z}
129
+  local pos = {x = cameraPos.x + movex, y = cameraPos.y + movey, z = cameraPos.z}
132 130
   minimapWidget:setCameraPosition(pos)
133 131
 end
134 132
 

+ 45
- 51
modules/game_minimap/minimap.otui View File

@@ -1,27 +1,24 @@
1 1
 MapControl < Button
2
-  size: 16 16
2
+  size: 20 20
3 3
 
4 4
   $pressed:
5
-    image-clip: 0 0 16 16
5
+    icon-clip: 0 0 16 16
6 6
 
7 7
   $hover !pressed:
8
-    image-clip: 0 16 16 16
8
+    icon-clip: 0 16 16 16
9 9
 
10 10
   $!pressed !hover:
11
-    image-clip: 0 32 16 16
12
-
13
-  $disabled:
14
-    image-color: #ffffff88
11
+    icon-clip: 0 32 16 16
15 12
 
16 13
 FloorUpControl < MapControl
17
-  image-source: /game_minimap/floor_up.png
18
-  
14
+  icon-source: /game_minimap/floor_up.png
15
+
19 16
 FloorDownControl < MapControl
20
-  image-source: /game_minimap/floor_down.png
21
-  
17
+  icon-source: /game_minimap/floor_down.png
18
+
22 19
 //ZoomOutControl < MapControl
23 20
   //image-source: /game_minimap/zoom_out.png
24
-  
21
+
25 22
 //ZoomInControl < MapControl
26 23
   //image-source: /game_minimap/zoom_in.png
27 24
 
@@ -37,81 +34,78 @@ MiniWindow
37 34
     padding: 3
38 35
     UIMap
39 36
       id: minimap
40
-      anchors.left: parent.left
41
-      anchors.top: parent.top
42
-      size: 110 110
43
-      
37
+      anchors.fill: parent
38
+
44 39
     UIButton
45 40
       id: compass
46 41
       image-source: /game_minimap/compass.png
42
+      image-smooth: true
47 43
       color: #ffffff
48
-      anchors.left: minimap.right
49
-      anchors.top: minimap.top
50
-      size: 60 60
51
-      margin-top: 3
52
-      margin-left: 5
44
+      anchors.left: parent.left
45
+      anchors.top: parent.top
46
+      size: 48 48
47
+      margin-top: 4
48
+      margin-left: 4
53 49
       @onClick: function(...) Minimap.compassClick(...) end
54
-      
50
+
55 51
     UIButton
56 52
       id: compassReset
57
-      text: Reset
53
+      text: R
58 54
       font: verdana-11px-rounded
59
-      anchors.left: compass.left
60
-      anchors.top: compass.top
61
-      margin-top: 25
62
-      margin-left: 14
55
+      anchors.centerIn: compass
56
+      margin-top: 2
57
+      margin-right: 1
63 58
       @onClick: function(...) Minimap.center(...) end
64 59
       $pressed:
65 60
         color: #eb9f4f88
66
-        margin-top: 26
67
-        margin-left: 15
68 61
 
69 62
       $hover !pressed:
70 63
         color: #eb9f4f
71 64
 
72 65
       $!pressed !hover:
73 66
         color: #ffffff
74
-        margin-top: 25
75
-        margin-left: 14
76 67
 
77 68
       $disabled:
78 69
         image-color: #ffffff88
79
-        
70
+
80 71
     FloorUpControl
81 72
       id: floorUp
82
-      anchors.left: minimap.right
83
-      anchors.top: compass.bottom
84
-      margin-top: 10
85
-      margin-left: 15
73
+      anchors.right: parent.right
74
+      anchors.bottom: parent.bottom
75
+      margin-right: 28
76
+      margin-bottom: 28
86 77
       enabled: true
87 78
       @onClick: Minimap.onButtonClick(self:getId())
88
-      
79
+
89 80
     FloorDownControl
90 81
       id: floorDown
91
-      anchors.left: floorUp.left
92
-      anchors.top: floorUp.bottom
93
-      margin-top: 4
82
+      anchors.right: parent.right
83
+      anchors.bottom: parent.bottom
84
+      margin-right: 28
85
+      margin-bottom: 4
94 86
       enabled: true
95 87
       @onClick: Minimap.onButtonClick(self:getId())
96
-      
88
+
97 89
     Button
98 90
       id: zoomIn
99 91
       text: +
100
-      size: 16 16
101
-      anchors.left: floorUp.right
102
-      anchors.top: floorUp.top
103
-      margin-left: 10
92
+      size: 20 20
93
+      anchors.right: parent.right
94
+      anchors.bottom: parent.bottom
95
+      margin-right: 4
96
+      margin-bottom: 28
104 97
       enabled: true
105 98
       @onClick: Minimap.onButtonClick(self:getId())
106
-      
99
+
107 100
     Button
108 101
       id: zoomOut
109 102
       text: -
110 103
       font: terminus-14px-bold
111
-      size: 16 16
112
-      anchors.left: floorDown.right
113
-      anchors.top: floorDown.top
114
-      margin-left: 10
104
+      size: 20 20
105
+      anchors.right: parent.right
106
+      anchors.bottom: parent.bottom
107
+      margin-right: 4
108
+      margin-bottom: 4
115 109
       enabled: true
116 110
       @onClick: Minimap.onButtonClick(self:getId())
117
-      
111
+

+ 2
- 0
src/framework/luafunctions.cpp View File

@@ -218,6 +218,7 @@ void Application::registerLuaFunctions()
218 218
     g_lua.bindClassMemberFunction<UIWidget>("setIconHeight", &UIWidget::setIconHeight);
219 219
     g_lua.bindClassMemberFunction<UIWidget>("setIconSize", &UIWidget::setIconSize);
220 220
     g_lua.bindClassMemberFunction<UIWidget>("setIconRect", &UIWidget::setIconRect);
221
+    g_lua.bindClassMemberFunction<UIWidget>("setIconClip", &UIWidget::setIconClip);
221 222
     g_lua.bindClassMemberFunction<UIWidget>("setBorderWidth", &UIWidget::setBorderWidth);
222 223
     g_lua.bindClassMemberFunction<UIWidget>("setBorderWidthTop", &UIWidget::setBorderWidthTop);
223 224
     g_lua.bindClassMemberFunction<UIWidget>("setBorderWidthRight", &UIWidget::setBorderWidthRight);
@@ -267,6 +268,7 @@ void Application::registerLuaFunctions()
267 268
     g_lua.bindClassMemberFunction<UIWidget>("getIconHeight", &UIWidget::getIconHeight);
268 269
     g_lua.bindClassMemberFunction<UIWidget>("getIconSize", &UIWidget::getIconSize);
269 270
     g_lua.bindClassMemberFunction<UIWidget>("getIconRect", &UIWidget::getIconRect);
271
+    g_lua.bindClassMemberFunction<UIWidget>("getIconClip", &UIWidget::getIconClip);
270 272
     g_lua.bindClassMemberFunction<UIWidget>("getBorderTopColor", &UIWidget::getBorderTopColor);
271 273
     g_lua.bindClassMemberFunction<UIWidget>("getBorderRightColor", &UIWidget::getBorderRightColor);
272 274
     g_lua.bindClassMemberFunction<UIWidget>("getBorderBottomColor", &UIWidget::getBorderBottomColor);

+ 3
- 0
src/framework/ui/uiwidget.h View File

@@ -278,6 +278,7 @@ protected:
278 278
     TexturePtr m_icon;
279 279
     Color m_iconColor;
280 280
     Rect m_iconRect;
281
+    Rect m_iconClipRect;
281 282
     EdgeGroup<Color> m_borderColor;
282 283
     EdgeGroup<int> m_borderWidth;
283 284
     EdgeGroup<int> m_margin;
@@ -311,6 +312,7 @@ public:
311 312
     void setIconHeight(int height) { m_iconRect.setHeight(height); }
312 313
     void setIconSize(const Size& size) { m_iconRect.resize(size); }
313 314
     void setIconRect(const Rect& rect) { m_iconRect = rect; }
315
+    void setIconClip(const Rect& rect) { m_iconClipRect = rect; }
314 316
     void setBorderWidth(int width) { m_borderWidth.set(width); updateLayout(); }
315 317
     void setBorderWidthTop(int width) { m_borderWidth.top = width; }
316 318
     void setBorderWidthRight(int width) { m_borderWidth.right = width; }
@@ -361,6 +363,7 @@ public:
361 363
     int getIconHeight() { return m_iconRect.height(); }
362 364
     Size getIconSize() { return m_iconRect.size(); }
363 365
     Rect getIconRect() { return m_iconRect; }
366
+    Rect getIconClip() { return m_iconClipRect; }
364 367
     Color getBorderTopColor() { return m_borderColor.top; }
365 368
     Color getBorderRightColor() { return m_borderColor.right; }
366 369
     Color getBorderBottomColor() { return m_borderColor.bottom; }

+ 6
- 2
src/framework/ui/uiwidgetbasestyle.cpp View File

@@ -100,6 +100,8 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
100 100
             setIconSize(node->value<Size>());
101 101
         else if(node->tag() == "icon-rect")
102 102
             setIconRect(node->value<Rect>());
103
+        else if(node->tag() == "icon-clip")
104
+            setIconClip(node->value<Rect>());
103 105
         else if(node->tag() == "opacity")
104 106
             setOpacity(node->value<float>());
105 107
         else if(node->tag() == "enabled")
@@ -369,15 +371,17 @@ void UIWidget::drawIcon(const Rect& screenCoords)
369 371
             drawRect.translate(m_iconRect.topLeft());
370 372
             drawRect.resize(m_iconRect.size());
371 373
         } else {
372
-            drawRect.resize(m_icon->getSize());
374
+            drawRect.resize(m_iconClipRect.size());
373 375
             drawRect.moveCenter(screenCoords.center());
374 376
         }
375 377
         g_painter->setColor(m_iconColor);
376
-        g_painter->drawTexturedRect(drawRect, m_icon);
378
+        g_painter->drawTexturedRect(drawRect, m_icon, m_iconClipRect);
377 379
     }
378 380
 }
379 381
 
380 382
 void UIWidget::setIcon(const std::string& iconFile)
381 383
 {
382 384
     m_icon = g_textures.getTexture(iconFile);
385
+    if(!m_iconClipRect.isValid())
386
+        m_iconClipRect = Rect(0, 0, m_icon->getSize());
383 387
 }

Loading…
Cancel
Save