summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-06-08 20:59:57 +0200
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-07-11 20:41:55 +0200
commit6b89b661df1a2682edfa491e1bb9a21c4ab0943c (patch)
tree11bbe888ecf9b45eeb78e371c00b950e9c4c4cac /src
parentb3836dd46658d8bc24af2d60c5e7904d7f051bca (diff)
downloadmana-6b89b661df1a2682edfa491e1bb9a21c4ab0943c.tar.gz
mana-6b89b661df1a2682edfa491e1bb9a21c4ab0943c.tar.bz2
mana-6b89b661df1a2682edfa491e1bb9a21c4ab0943c.tar.xz
mana-6b89b661df1a2682edfa491e1bb9a21c4ab0943c.zip
Cleaned up ImageRect a little
* Use `std::unique_ptr`, so we can get rid of the custom move constructor and destructor. * Move and rename the `ImagePosition` enum to `WindowAlignment`, which fits better with what this enum is actually used for.
Diffstat (limited to 'src')
-rw-r--r--src/graphics.cpp23
-rw-r--r--src/graphics.h54
-rw-r--r--src/gui/buydialog.cpp2
-rw-r--r--src/gui/chatwindow.cpp2
-rw-r--r--src/gui/equipmentwindow.cpp2
-rw-r--r--src/gui/helpwindow.cpp2
-rw-r--r--src/gui/inventorywindow.cpp2
-rw-r--r--src/gui/npcdialog.cpp2
-rw-r--r--src/gui/questswindow.cpp2
-rw-r--r--src/gui/recorder.cpp2
-rw-r--r--src/gui/selldialog.cpp2
-rw-r--r--src/gui/serverdialog.cpp2
-rw-r--r--src/gui/shortcutwindow.cpp2
-rw-r--r--src/gui/tradewindow.cpp2
-rw-r--r--src/gui/updaterwindow.cpp2
-rw-r--r--src/gui/widgets/window.cpp68
-rw-r--r--src/gui/widgets/window.h21
-rw-r--r--src/resources/theme.cpp2
18 files changed, 59 insertions, 135 deletions
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 5455f61a..c5b86bc6 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -21,27 +21,8 @@
#include "graphics.h"
-#include "resources/image.h"
-
#include <guichan/exception.hpp>
-#include <utility>
-
-ImageRect::ImageRect(ImageRect &&r)
-{
- image = std::exchange(r.image, nullptr);
- top = r.top;
- left = r.left;
- bottom = r.bottom;
- right = r.right;
- fillMode = r.fillMode;
-}
-
-ImageRect::~ImageRect()
-{
- delete image;
-}
-
void Graphics::updateSize(int width, int height, float /*scale*/)
{
@@ -166,7 +147,7 @@ void Graphics::drawImageRect(const ImageRect &imgRect, int x, int y, int w, int
switch (imgRect.fillMode)
{
case FillMode::Stretch:
- drawRescaledImage(imgRect.image,
+ drawRescaledImage(imgRect.image.get(),
srcGridX[ix],
srcGridY[iy],
dstGridX[ix],
@@ -175,7 +156,7 @@ void Graphics::drawImageRect(const ImageRect &imgRect, int x, int y, int w, int
dstW, dstH);
break;
case FillMode::Repeat:
- drawRescaledImagePattern(imgRect.image,
+ drawRescaledImagePattern(imgRect.image.get(),
srcGridX[ix],
srcGridY[iy],
srcW, srcH,
diff --git a/src/graphics.h b/src/graphics.h
index 038b0c6b..9d8a9215 100644
--- a/src/graphics.h
+++ b/src/graphics.h
@@ -21,12 +21,14 @@
#pragma once
+#include "resources/image.h"
+
#include <SDL.h>
#include <guichan/color.hpp>
#include <guichan/graphics.hpp>
-class Image;
+#include <memory>
enum class FillMode
{
@@ -35,47 +37,27 @@ enum class FillMode
};
/**
- * 9 images defining a rectangle. 4 corners, 4 sides and a middle area. The
- * topology is as follows:
+ * An image reference along with the margins specifying how to render this
+ * image at different sizes. The margins divide the image into 9 sections as
+ * follows:
*
* <pre>
- * !-----!-----------------!-----!
- * ! 0 ! 1 ! 2 !
- * !-----!-----------------!-----!
- * ! 3 ! 4 ! 5 !
- * !-----!-----------------!-----!
- * ! 6 ! 7 ! 8 !
- * !-----!-----------------!-----!
+ * !------!--------------!-------!
+ * ! ! top ! !
+ * !------!--------------!-------!
+ * ! left ! ! right !
+ * !------!--------------!-------!
+ * ! ! bottom ! !
+ * !------!--------------!-------!
* </pre>
*
- * Sections 0, 2, 6 and 8 will remain as is. 1, 3, 4, 5 and 7 will be
- * repeated to fit the size of the widget.
+ * The corner sections will remain as is. The edges and the center sections
+ * will be repeated or stretched to fit the target size, depending on the fill
+ * mode.
*/
-class ImageRect
+struct ImageRect
{
-public:
- enum ImagePosition
- {
- UPPER_LEFT = 0,
- UPPER_CENTER = 1,
- UPPER_RIGHT = 2,
- LEFT = 3,
- CENTER = 4,
- RIGHT = 5,
- LOWER_LEFT = 6,
- LOWER_CENTER = 7,
- LOWER_RIGHT = 8
- };
-
- ImageRect() = default;
- ImageRect(const ImageRect &) = delete;
- ImageRect(ImageRect &&);
- ~ImageRect();
-
- ImageRect &operator=(const ImageRect &) = delete;
- ImageRect &operator=(ImageRect &&r) = delete;
-
- Image *image = nullptr;
+ std::unique_ptr<Image> image;
int top = 0;
int left = 0;
int bottom = 0;
diff --git a/src/gui/buydialog.cpp b/src/gui/buydialog.cpp
index 135c2119..fb316722 100644
--- a/src/gui/buydialog.cpp
+++ b/src/gui/buydialog.cpp
@@ -54,7 +54,7 @@ BuyDialog::BuyDialog(int npcId):
setCloseButton(true);
setMinWidth(260);
setMinHeight(230);
- setDefaultSize(260, 230, ImageRect::CENTER);
+ setDefaultSize(260, 230, WindowAlignment::Center);
mShopItems = new ShopItems;
diff --git a/src/gui/chatwindow.cpp b/src/gui/chatwindow.cpp
index d006097c..d19231ab 100644
--- a/src/gui/chatwindow.cpp
+++ b/src/gui/chatwindow.cpp
@@ -102,7 +102,7 @@ ChatWindow::ChatWindow():
setResizable(true);
setDefaultVisible(true);
setSaveVisible(true);
- setDefaultSize(600, 123, ImageRect::LOWER_LEFT);
+ setDefaultSize(600, 123, WindowAlignment::BottomLeft);
setMinWidth(150);
setMinHeight(90);
diff --git a/src/gui/equipmentwindow.cpp b/src/gui/equipmentwindow.cpp
index b9d3b0fd..8523e7b2 100644
--- a/src/gui/equipmentwindow.cpp
+++ b/src/gui/equipmentwindow.cpp
@@ -63,7 +63,7 @@ EquipmentWindow::EquipmentWindow(Equipment *equipment):
setCloseButton(true);
setSaveVisible(true);
setContentSize(175, 290);
- setDefaultSize(getWidth(), getHeight(), ImageRect::CENTER);
+ setDefaultSize(getWidth(), getHeight(), WindowAlignment::Center);
loadWindowState();
mUnequip = new Button(_("Unequip"), "unequip", this);
diff --git a/src/gui/helpwindow.cpp b/src/gui/helpwindow.cpp
index c7e87a55..7c7c5d4c 100644
--- a/src/gui/helpwindow.cpp
+++ b/src/gui/helpwindow.cpp
@@ -45,7 +45,7 @@ HelpWindow::HelpWindow():
setResizable(true);
setupWindow->registerWindowForReset(this);
- setDefaultSize(500, 400, ImageRect::CENTER);
+ setDefaultSize(500, 400, WindowAlignment::Center);
mBrowserBox = new BrowserBox;
mScrollArea = new ScrollArea(mBrowserBox);
diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp
index ab2e9c86..0125700c 100644
--- a/src/gui/inventorywindow.cpp
+++ b/src/gui/inventorywindow.cpp
@@ -65,7 +65,7 @@ InventoryWindow::InventoryWindow(Inventory *inventory):
setCloseButton(true);
setSaveVisible(true);
- setDefaultSize(387, 307, ImageRect::CENTER);
+ setDefaultSize(387, 307, WindowAlignment::Center);
setMinWidth(316);
setMinHeight(179);
addKeyListener(this);
diff --git a/src/gui/npcdialog.cpp b/src/gui/npcdialog.cpp
index 16e7db94..033d01cc 100644
--- a/src/gui/npcdialog.cpp
+++ b/src/gui/npcdialog.cpp
@@ -82,7 +82,7 @@ NpcDialog::NpcDialog(int npcId)
setMinWidth(200);
setMinHeight(150);
- setDefaultSize(260, 200, ImageRect::CENTER);
+ setDefaultSize(260, 200, WindowAlignment::Center);
// Setup output text box
mTextBox = new BrowserBox(BrowserBox::AUTO_WRAP);
diff --git a/src/gui/questswindow.cpp b/src/gui/questswindow.cpp
index 99d6bd6d..adbbdeda 100644
--- a/src/gui/questswindow.cpp
+++ b/src/gui/questswindow.cpp
@@ -144,7 +144,7 @@ QuestsWindow::QuestsWindow()
setCloseButton(true);
setSaveVisible(true);
- setDefaultSize(387, 307, ImageRect::CENTER);
+ setDefaultSize(387, 307, WindowAlignment::Center);
setMinWidth(316);
setMinHeight(179);
diff --git a/src/gui/recorder.cpp b/src/gui/recorder.cpp
index 894e3631..96e458ff 100644
--- a/src/gui/recorder.cpp
+++ b/src/gui/recorder.cpp
@@ -47,7 +47,7 @@ Recorder::Recorder(ChatWindow *chat,
// 123 is the default chat window height. If you change this in Chat, please
// change it here as well
setDefaultSize(button->getWidth() + offsetX, button->getHeight() +
- offsetY, ImageRect::LOWER_LEFT, 0, 123);
+ offsetY, WindowAlignment::BottomLeft, 0, 123);
place(0, 0, button);
diff --git a/src/gui/selldialog.cpp b/src/gui/selldialog.cpp
index 4aeacd6f..4fa12e53 100644
--- a/src/gui/selldialog.cpp
+++ b/src/gui/selldialog.cpp
@@ -56,7 +56,7 @@ SellDialog::SellDialog(int npcId):
setCloseButton(true);
setMinWidth(260);
setMinHeight(230);
- setDefaultSize(260, 230, ImageRect::CENTER);
+ setDefaultSize(260, 230, WindowAlignment::Center);
// Create a ShopItems instance, that is aware of duplicate entries.
mShopItems = new ShopItems(true);
diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp
index 8be33733..872ecb28 100644
--- a/src/gui/serverdialog.cpp
+++ b/src/gui/serverdialog.cpp
@@ -201,7 +201,7 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir):
setMinWidth(getWidth());
setMinHeight(getHeight());
- setDefaultSize(getWidth(), getHeight(), ImageRect::CENTER);
+ setDefaultSize(getWidth(), getHeight(), WindowAlignment::Center);
setResizable(true);
addKeyListener(this);
diff --git a/src/gui/shortcutwindow.cpp b/src/gui/shortcutwindow.cpp
index 78a22335..c33fcf01 100644
--- a/src/gui/shortcutwindow.cpp
+++ b/src/gui/shortcutwindow.cpp
@@ -55,7 +55,7 @@ ShortcutWindow::ShortcutWindow(const std::string &title,
setMaxWidth(std::max(getMinWidth(), maxContentWidth + border));
setMaxHeight(std::max(getMinHeight(), maxContentHeight + border + GRAB_MARGIN));
- setDefaultSize(getMinWidth(), getMaxHeight(), ImageRect::LOWER_RIGHT);
+ setDefaultSize(getMinWidth(), getMaxHeight(), WindowAlignment::BottomRight);
place(0, 0, scrollArea, 5, 5).setPadding(0);
diff --git a/src/gui/tradewindow.cpp b/src/gui/tradewindow.cpp
index e24845b7..6610bd43 100644
--- a/src/gui/tradewindow.cpp
+++ b/src/gui/tradewindow.cpp
@@ -60,7 +60,7 @@ TradeWindow::TradeWindow():
setWindowName("Trade");
setResizable(true);
setCloseButton(true);
- setDefaultSize(386, 180, ImageRect::CENTER);
+ setDefaultSize(386, 180, WindowAlignment::Center);
setMinWidth(386);
setMinHeight(180);
setupWindow->registerWindowForReset(this);
diff --git a/src/gui/updaterwindow.cpp b/src/gui/updaterwindow.cpp
index bb9523a9..c16c3e04 100644
--- a/src/gui/updaterwindow.cpp
+++ b/src/gui/updaterwindow.cpp
@@ -128,7 +128,7 @@ UpdaterWindow::UpdaterWindow(const std::string &updateHost,
{
setWindowName("UpdaterWindow");
setResizable(true);
- setDefaultSize(450, 400, ImageRect::CENTER);
+ setDefaultSize(450, 400, WindowAlignment::Center);
setMinWidth(320);
setMinHeight(240);
diff --git a/src/gui/widgets/window.cpp b/src/gui/widgets/window.cpp
index b05731bd..bf496bd8 100644
--- a/src/gui/widgets/window.cpp
+++ b/src/gui/widgets/window.cpp
@@ -191,52 +191,6 @@ void Window::setLocationRelativeTo(gcn::Widget *widget)
getY() + (wy + (widget->getHeight() - getHeight()) / 2 - y));
}
-void Window::setLocationRelativeTo(ImageRect::ImagePosition position,
- int offsetX, int offsetY)
-{
- if (position == ImageRect::UPPER_LEFT)
- {
- }
- else if (position == ImageRect::UPPER_CENTER)
- {
- offsetX += (graphics->getWidth() - getWidth()) / 2;
- }
- else if (position == ImageRect::UPPER_RIGHT)
- {
- offsetX += graphics->getWidth() - getWidth();
- }
- else if (position == ImageRect::LEFT)
- {
- offsetY += (graphics->getHeight() - getHeight()) / 2;
- }
- else if (position == ImageRect::CENTER)
- {
- offsetX += (graphics->getWidth() - getWidth()) / 2;
- offsetY += (graphics->getHeight() - getHeight()) / 2;
- }
- else if (position == ImageRect::RIGHT)
- {
- offsetX += graphics->getWidth() - getWidth();
- offsetY += (graphics->getHeight() - getHeight()) / 2;
- }
- else if (position == ImageRect::LOWER_LEFT)
- {
- offsetY += graphics->getHeight() - getHeight();
- }
- else if (position == ImageRect::LOWER_CENTER)
- {
- offsetX += (graphics->getWidth() - getWidth()) / 2;
- offsetY += graphics->getHeight() - getHeight();
- }
- else if (position == ImageRect::LOWER_RIGHT)
- {
- offsetX += graphics->getWidth() - getWidth();
- offsetY += graphics->getHeight() - getHeight();
- }
-
- setPosition(offsetX, offsetY);
-}
-
void Window::setMinWidth(int width)
{
mMinWinWidth = std::max(getSkin().getMinWidth(), width);
@@ -599,41 +553,41 @@ void Window::setDefaultSize()
}
void Window::setDefaultSize(int defaultWidth, int defaultHeight,
- ImageRect::ImagePosition position,
+ WindowAlignment alignment,
int offsetX, int offsetY)
{
int x = 0;
int y = 0;
- switch (position)
+ switch (alignment)
{
- case ImageRect::UPPER_LEFT:
+ case WindowAlignment::TopLeft:
break;
- case ImageRect::UPPER_CENTER:
+ case WindowAlignment::Top:
x = (graphics->getWidth() - defaultWidth) / 2;
break;
- case ImageRect::UPPER_RIGHT:
+ case WindowAlignment::TopRight:
x = graphics->getWidth() - defaultWidth;
break;
- case ImageRect::LEFT:
+ case WindowAlignment::Left:
y = (graphics->getHeight() - defaultHeight) / 2;
break;
- case ImageRect::CENTER:
+ case WindowAlignment::Center:
x = (graphics->getWidth() - defaultWidth) / 2;
y = (graphics->getHeight() - defaultHeight) / 2;
break;
- case ImageRect::RIGHT:
+ case WindowAlignment::Right:
x = graphics->getWidth() - defaultWidth;
y = (graphics->getHeight() - defaultHeight) / 2;
break;
- case ImageRect::LOWER_LEFT:
+ case WindowAlignment::BottomLeft:
y = graphics->getHeight() - defaultHeight;
break;
- case ImageRect::LOWER_CENTER:
+ case WindowAlignment::Bottom:
x = (graphics->getWidth() - defaultWidth) / 2;
y = graphics->getHeight() - defaultHeight;
break;
- case ImageRect::LOWER_RIGHT:
+ case WindowAlignment::BottomRight:
x = graphics->getWidth() - defaultWidth;
y = graphics->getHeight() - defaultHeight;
break;
diff --git a/src/gui/widgets/window.h b/src/gui/widgets/window.h
index 6331a715..2a47b0b9 100644
--- a/src/gui/widgets/window.h
+++ b/src/gui/widgets/window.h
@@ -36,6 +36,19 @@ class ResizeGrip;
class Skin;
class WindowContainer;
+enum class WindowAlignment
+{
+ TopLeft,
+ Top,
+ TopRight,
+ Left,
+ Center,
+ Right,
+ BottomLeft,
+ Bottom,
+ BottomRight
+};
+
/**
* A window. This window can be dragged around and has a title bar. Windows are
* invisible by default.
@@ -103,12 +116,6 @@ class Window : public gcn::Window, gcn::WidgetListener
void setLocationRelativeTo(gcn::Widget *widget);
/**
- * Sets the location relative to the given enumerated position.
- */
- void setLocationRelativeTo(ImageRect::ImagePosition position,
- int offsetX = 0, int offsetY = 0);
-
- /**
* Sets whether or not the window can be resized.
*/
void setResizable(bool resize);
@@ -303,7 +310,7 @@ class Window : public gcn::Window, gcn::WidgetListener
* on a relative enumerated position, rather than a coordinate position.
*/
void setDefaultSize(int defaultWidth, int defaultHeight,
- ImageRect::ImagePosition position,
+ WindowAlignment alignment,
int offsetx = 0, int offsetY = 0);
/**
diff --git a/src/resources/theme.cpp b/src/resources/theme.cpp
index 2845f91e..fa5f1a7d 100644
--- a/src/resources/theme.cpp
+++ b/src/resources/theme.cpp
@@ -695,7 +695,7 @@ void Theme::readSkinStateImgNode(XML::Node node, SkinState &state) const
border.right = right;
border.top = top;
border.bottom = bottom;
- border.image = image->getSubImage(x, y, width, height);
+ border.image.reset(image->getSubImage(x, y, width, height));
node.attribute("fill", border.fillMode);
}