From 546007f1df54c988c8289e9637a0c87596eb54ba Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Thu, 31 Jan 2013 14:20:04 -0200 Subject: [PATCH] UIImageView, fixes to minimap --- data/styles/20-imageview.otui | 6 ++ data/styles/30-minimap.otui | 10 ++-- modules/corelib/ui/uiimageview.lua | 96 ++++++++++++++++++++++++++++++ modules/game_minimap/minimap.lua | 3 +- modules/gamelib/ui/uiminimap.lua | 26 +++++--- src/framework/luafunctions.cpp | 2 + src/framework/ui/uiwidget.h | 2 + 7 files changed, 130 insertions(+), 15 deletions(-) create mode 100644 data/styles/20-imageview.otui create mode 100644 modules/corelib/ui/uiimageview.lua diff --git a/data/styles/20-imageview.otui b/data/styles/20-imageview.otui new file mode 100644 index 00000000..73bda84c --- /dev/null +++ b/data/styles/20-imageview.otui @@ -0,0 +1,6 @@ +ImageView < UIImageView + image-smooth: false + image-fixed-ratio: true + draggable: true + border-width: 2 + border-color: #000000 diff --git a/data/styles/30-minimap.otui b/data/styles/30-minimap.otui index da6664c8..654af8dc 100644 --- a/data/styles/30-minimap.otui +++ b/data/styles/30-minimap.otui @@ -248,13 +248,15 @@ MinimapFlagWindow < MainWindow // Minimap Full Panel -MinimapFullPanel < UIWidget - image-smooth: true - border-width: 2 - border-color: #000000 +MinimapFullPanel < FlatPanel + phantom: false anchors.fill: parent anchors.top: topMenu.bottom + ImageView + id: image + anchors.fill: parent + Button !text: tr('Close') margin-right: 4 diff --git a/modules/corelib/ui/uiimageview.lua b/modules/corelib/ui/uiimageview.lua new file mode 100644 index 00000000..85e5c1a4 --- /dev/null +++ b/modules/corelib/ui/uiimageview.lua @@ -0,0 +1,96 @@ +-- @docclass +UIImageView = extends(UIWidget) + +function UIImageView.create() + local imageView = UIImageView.internalCreate() + imageView.zoom = 1 + imageView:setClipping(true) + return imageView +end + +function UIImageView:getDefaultZoom() + local width = self:getWidth() + local height = self:getHeight() + local textureWidth = self:getImageTextureWidth() + local textureHeight = self:getImageTextureHeight() + local zoomX = width / textureWidth + local zoomY = height / textureHeight + return math.min(zoomX, zoomY) +end + +function UIImageView:getImagePosition(x, y) + x = x or self:getWidth() / 2 + y = y or self:getHeight() / 2 + local offsetX = self:getImageOffsetX() + local offsetY = self:getImageOffsetY() + local posX = (x - offsetX) / self.zoom + local posY = (y - offsetY) / self.zoom + return posX, posY +end + +function UIImageView:setImage(image) + self:setImageSource(image) + local zoom = self:getDefaultZoom() + self:setZoom(zoom) + self:center() +end + +function UIImageView:setZoom(zoom, x, y) + local posX, posY = self:getImagePosition(x, y) + local textureWidth = self:getImageTextureWidth() + local textureHeight = self:getImageTextureHeight() + local imageWidth = textureWidth * zoom + local imageHeight = textureHeight * zoom + self:setImageWidth(imageWidth) + self:setImageHeight(imageHeight) + self.zoom = zoom + self:move(posX, posY, x, y) +end + +function UIImageView:zoomIn(x, y) + local zoom = self.zoom * 1.1 + self:setZoom(zoom, x, y) +end + +function UIImageView:zoomOut(x, y) + local zoom = self.zoom / 1.1 + self:setZoom(zoom, x, y) +end + +function UIImageView:center() + self:move(self:getImageTextureWidth() / 2, self:getImageTextureHeight() / 2) +end + +function UIImageView:move(x, y, centerX, centerY) + x = math.max(math.min(x, self:getImageTextureWidth()), 0) + y = math.max(math.min(y, self:getImageTextureHeight()), 0) + local centerX = centerX or self:getWidth() / 2 + local centerY = centerY or self:getHeight() / 2 + local offsetX = centerX - x * self.zoom + local offsetY = centerY - y * self.zoom + self:setImageOffset({x=offsetX, y=offsetY}) +end + +function UIImageView:onDragEnter(pos) + return true +end + +function UIImageView:onDragMove(pos, moved) + local posX, posY = self:getImagePosition() + self:move(posX - moved.x / self.zoom, posY - moved.y / self.zoom) + return true +end + +function UIImageView:onDragLeave(widget, pos) + return true +end + +function UIImageView:onMouseWheel(mousePos, direction) + local x = mousePos.x - self:getX() + local y = mousePos.y - self:getY() + if direction == MouseWheelUp then + self:zoomIn(x, y) + elseif direction == MouseWheelDown then + self:zoomOut(x, y) + end +end diff --git a/modules/game_minimap/minimap.lua b/modules/game_minimap/minimap.lua index c3f421a9..95bc93cd 100644 --- a/modules/game_minimap/minimap.lua +++ b/modules/game_minimap/minimap.lua @@ -27,7 +27,7 @@ function init() }) if g_game.isOnline() then - loadMap() + online() end end @@ -77,7 +77,6 @@ end function loadMap() local protocolVersion = g_game.getProtocolVersion() - g_map.clean() g_minimap.clean() if otmm then diff --git a/modules/gamelib/ui/uiminimap.lua b/modules/gamelib/ui/uiminimap.lua index f98afcda..54221a36 100644 --- a/modules/gamelib/ui/uiminimap.lua +++ b/modules/gamelib/ui/uiminimap.lua @@ -146,18 +146,24 @@ function UIMinimap:updateFlags() end end -UIMinimap.realZoomIn = UIMinimap.zoomIn +UIMinimap.realZoomIn = UIMinimap.realZoomIn or UIMinimap.zoomIn function UIMinimap:zoomIn() self:realZoomIn() self:updateFlags() end -UIMinimap.realZoomOut = UIMinimap.zoomOut +UIMinimap.realZoomOut = UIMinimap.realZoomOut or UIMinimap.zoomOut function UIMinimap:zoomOut() self:realZoomOut() self:updateFlags() end +UIMinimap.realSetZoom = UIMinimap.realSetZoom or UIMinimap.setZoom +function UIMinimap:setZoom(zoom) + self:realSetZoom(zoom) + self:updateFlags() +end + function UIMinimap:floorUp(floors) local pos = self:getCameraPosition() pos.z = pos.z - floors @@ -177,9 +183,11 @@ function UIMinimap:floorDown(floors) end function UIMinimap:followLocalPlayer() - local player = g_game.getLocalPlayer() - self:followCreature(player) - self:updateFlags() + if not self:isDragging() then + local player = g_game.getLocalPlayer() + self:followCreature(player) + self:updateFlags() + end end function UIMinimap:reset() @@ -231,8 +239,9 @@ function UIMinimap:onMouseRelease(pos, button) if button == MouseLeftButton then local player = g_game.getLocalPlayer() - if self.autowalk and not player:autoWalk(mapPos) then + if self.autowalk then player.onAutoWalkFail = function() modules.game_textmessage.displayFailureMessage(tr('There is no way.')) end + player:autoWalk(mapPos) end return true elseif button == MouseRightButton then @@ -260,8 +269,9 @@ end function UIMinimap:createFullPanel() self.fullPanel = g_ui.createWidget('MinimapFullPanel', rootWidget) - self.fullPanel:setImageSource(self.fullImage) self.fullPanel.onDestroy = function() self.fullPanel = nil end + local image = self.fullPanel:getChildById('image') + image:setImage(self.fullImage) end function UIMinimap:destroyFullPanel() @@ -316,5 +326,3 @@ function UIMinimap:getArea() local bottomRight = self:getPosition({ x = self:getX() + self:getWidth() - 2, y = self:getY() + self:getHeight() - 2 }) return topLeft, bottomRight end - - diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 29b67bd9..c5b0e731 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -581,6 +581,8 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("getImageBorderRight", &UIWidget::getImageBorderRight); g_lua.bindClassMemberFunction("getImageBorderBottom", &UIWidget::getImageBorderBottom); g_lua.bindClassMemberFunction("getImageBorderLeft", &UIWidget::getImageBorderLeft); + g_lua.bindClassMemberFunction("getImageTextureWidth", &UIWidget::getImageTextureWidth); + g_lua.bindClassMemberFunction("getImageTextureHeight", &UIWidget::getImageTextureHeight); g_lua.bindClassMemberFunction("resizeToText", &UIWidget::resizeToText); g_lua.bindClassMemberFunction("clearText", &UIWidget::clearText); g_lua.bindClassMemberFunction("setText", &UIWidget::setText); diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h index 5db9c651..805677cb 100644 --- a/src/framework/ui/uiwidget.h +++ b/src/framework/ui/uiwidget.h @@ -455,6 +455,8 @@ public: int getImageBorderRight() { return m_imageBorder.right; } int getImageBorderBottom() { return m_imageBorder.bottom; } int getImageBorderLeft() { return m_imageBorder.left; } + int getImageTextureWidth() { return m_imageTexture ? m_imageTexture->getWidth() : 0; } + int getImageTextureHeight() { return m_imageTexture ? m_imageTexture->getHeight() : 0; } // text related private: