Skip to content

Commit 4d7e43a

Browse files
committed
增加ImageBox、
1 parent aa9449d commit 4d7e43a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1902
-70
lines changed

application/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ add_subdirectory(chapter-27)
2929
add_subdirectory(chapter-28)
3030
add_subdirectory(chapter-29)
3131
add_subdirectory(chapter-30)
32+
add_subdirectory(chapter-31)
33+
add_subdirectory(chapter-32)

application/chapter-00/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-00 main.cpp)
3+
target_link_libraries(chapter-00 framework sfml-system sfml-graphics sfml-window)

application/chapter-00/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <SFML/Window.hpp>
2+
#include <SFML/Graphics.hpp>
3+
4+
int main()
5+
{
6+
sf::RenderWindow window(sf::VideoMode(800, 600), "Chapter-00");
7+
8+
while (window.isOpen()) {
9+
sf::Event event;
10+
while (window.pollEvent(event)) {
11+
if (event.type == sf::Event::Closed)
12+
window.close();
13+
}
14+
15+
window.display();
16+
}
17+
18+
return 0;
19+
}
20+

application/chapter-23/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class HelpListener : public MessageListener
1414
HelpListener()
1515
{
1616
helpLabel = std::make_shared<Label>();
17-
helpLabel->setSize(320, 108);
17+
helpLabel->setSize(300, 108);
1818
helpLabel->setText(L"帮助\n点击键盘方向键控制坦克旋转\n点击上键控制坦克前进\n点击空格弹出本提示框");
1919

2020
auto style = std::make_shared<LabelStyle>();

application/chapter-24/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <Application.h>
33
#include <Text.h>
44
#include <Scene.h>
5+
#include <ResourceManager.h>
56
#include <SoundManager.h>
67
#include <Widget/Desktop.h>
78
#include <Widget/BoxLayout.h>
@@ -42,7 +43,8 @@ int main()
4243
app->setWindow(window);
4344

4445
auto scene = std::make_shared<Scene>();
45-
auto image = Application::getInstance()->loadImage("../resource/images/tank/background.png");
46+
auto imageManager = Application::getInstance()->getComponent<ResourceManager<sf::Image>>();
47+
auto image = imageManager->loadFromFile("../resource/images/tank/background.png");
4648

4749
sf::Texture texture;
4850
texture.loadFromImage(*image);

application/chapter-25/WayFindingTank.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <SFML/Graphics/Texture.hpp>
12
#include <Application.h>
23
#include "SpriteForwardState.h"
34
#include "WayFindingTank.h"

application/chapter-29/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-29 main.cpp)
3+
target_link_libraries(chapter-29 framework sfml-system sfml-graphics sfml-window)

application/chapter-29/main.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <iostream>
2+
#include <SFML/Graphics/RenderWindow.hpp>
3+
#include <Application.h>
4+
#include <Entity.h>
5+
#include <NameHolder.h>
6+
7+
using namespace std;
8+
9+
class Unit : public Entity, public NameHolder
10+
{
11+
public:
12+
Unit(const sf::Vector2f &size = sf::Vector2f()):
13+
Entity(size)
14+
{
15+
setOutlineColor(sf::Color::Yellow);
16+
setOutlineThickness(1.0f);
17+
setBackgroundColor(sf::Color::Green);
18+
}
19+
private:
20+
void onMouseEnter()
21+
{
22+
setBackgroundColor(sf::Color::Red);
23+
}
24+
25+
void onMouseExit()
26+
{
27+
setBackgroundColor(sf::Color::Green);
28+
}
29+
30+
void onMousePressed(sf::Mouse::Button button)
31+
{
32+
(void)button;
33+
setBackgroundColor(sf::Color::White);
34+
}
35+
36+
void onMouseReleased(sf::Mouse::Button button)
37+
{
38+
(void)button;
39+
setBackgroundColor(sf::Color::Black);
40+
}
41+
42+
void onMouseWheelScroll(float scroll)
43+
{
44+
(void)scroll;
45+
}
46+
47+
void onMouseMoved(int x, int y)
48+
{
49+
(void)x, (void)y;
50+
}
51+
};
52+
53+
int main()
54+
{
55+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(600, 600), "Chapter-29",
56+
sf::Style::Close);
57+
window->setVerticalSyncEnabled(true);
58+
59+
auto app = Application::getInstance();
60+
app->setBackgroundColor(sf::Color::Blue);
61+
app->setWindow(window);
62+
63+
auto parent = std::make_shared<Unit>();
64+
parent->setName("parent");
65+
parent->setSize(300, 300);
66+
parent->setPosition(150, 150);
67+
parent->setBackgroundColor(sf::Color::Black);
68+
69+
auto object = std::make_shared<Unit>();
70+
object->setName("object");
71+
object->setSize(200, 200);
72+
object->setPosition(100, 100);
73+
object->setBackgroundColor(sf::Color::Blue);
74+
75+
parent->addChild(object);
76+
77+
auto child = std::make_shared<Unit>();
78+
child->setName("child");
79+
child->setSize(100, 100);
80+
child->setPosition(100, 100);
81+
child->setBackgroundColor(sf::Color::Yellow);
82+
object->addChild(child);
83+
84+
app->execute(parent);
85+
return 0;
86+
}

application/chapter-30/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-30 main.cpp)
3+
target_link_libraries(chapter-30 framework sfml-audio sfml-system sfml-graphics sfml-window)

application/chapter-30/main.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <SFML/Graphics/RenderWindow.hpp>
2+
#include <SFML/Graphics/View.hpp>
3+
#include <Application.h>
4+
#include <Entity.h>
5+
#include <TileMap.h>
6+
#include <Scene.h>
7+
#include <Text.h>
8+
#include <ResourceManager.h>
9+
10+
using namespace std;
11+
12+
class MouseListener : public MessageListener
13+
{
14+
public:
15+
MouseListener(TileMapPointer map)
16+
{
17+
tileMap = map;
18+
}
19+
20+
bool onListener(std::shared_ptr<Message> message) override
21+
{
22+
if (message->getType() == Message_SFML) {
23+
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
24+
auto mousePosition = sf::Mouse::getPosition(*Application::getInstance()->getWindow());
25+
auto index = tileMap.lock()->getTileIndexByWorldPosition(mousePosition.x, mousePosition.y);
26+
auto tile = tileMap.lock()->getTileByIndex(index);
27+
if (tile)
28+
tile->setOutlineColor(sf::Color::Yellow);
29+
}
30+
return true;
31+
}
32+
33+
return false;
34+
}
35+
private:
36+
std::weak_ptr<TileMap> tileMap;
37+
};
38+
39+
class RectMapScene : public Scene, public TileVisitor
40+
{
41+
public:
42+
RectMapScene()
43+
{
44+
tileMap = TileMap::createTileMap(TileMapType_Grid);
45+
tileMap->init(19, 19, 64);
46+
47+
addChild(tileMap);
48+
49+
setName("scene");
50+
auto text = createToastText();
51+
text->setText(L"瓦片地图", false);
52+
addChild(text);
53+
54+
auto imageManager = Application::getInstance()->getComponent<ResourceManager<sf::Image>>();
55+
56+
tileSet = *imageManager->loadFromFile("../resource/images/tileset.png");
57+
58+
for(int i = 0; i < 8; i++)
59+
for(int j = 0; j < 6; j++) {
60+
sf::IntRect rect;
61+
rect.left = i * 32;
62+
rect.top = j * 32;
63+
rect.width = 32;
64+
rect.height = 32;
65+
66+
std::shared_ptr<sf::Texture> texture = std::make_shared<sf::Texture>();
67+
texture->loadFromImage(tileSet, rect);
68+
textures.push_back(texture);
69+
}
70+
71+
tileMap->accept(this);
72+
73+
auto listener = std::make_shared<MouseListener>(tileMap);
74+
tileMap->addMessageListener(listener);
75+
}
76+
77+
void visit(uint32_t x, uint32_t y, std::shared_ptr<Tile> tile) override
78+
{
79+
(void)x, (void)y;
80+
tile->setScale(0.9f, 0.9f);
81+
tile->setVisible(true);
82+
tile->setFillColor(sf::Color::White);
83+
int id = rand() % textures.size();
84+
tile->setTexture(textures.at(id).get());
85+
}
86+
private:
87+
std::shared_ptr<TileMap> tileMap;
88+
sf::Image tileSet;
89+
std::vector<std::shared_ptr<sf::Texture>> textures;
90+
};
91+
92+
int main()
93+
{
94+
auto size = sf::Vector2f(960, 720);
95+
auto setting = sf::ContextSettings();
96+
setting.antialiasingLevel = 12;
97+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-30",
98+
sf::Style::Close, setting);
99+
window->setVerticalSyncEnabled(true);
100+
101+
auto app = Application::getInstance();
102+
app->setWindow(window);
103+
104+
auto scene = std::make_shared<RectMapScene>();
105+
app->execute(scene);
106+
return 0;
107+
}
108+

application/chapter-31/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-31 main.cpp)
3+
target_link_libraries(chapter-31 framework sfml-audio sfml-system sfml-graphics sfml-window)

application/chapter-31/main.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <SFML/Graphics/RenderWindow.hpp>
2+
#include <Application.h>
3+
#include <Text.h>
4+
#include <Scene.h>
5+
#include <SoundManager.h>
6+
#include <ResourceManager.h>
7+
#include <Widget/Desktop.h>
8+
#include <Widget/TableLayout.h>
9+
#include <Widget/ImageBox.h>
10+
#include <Widget/Panel.h>
11+
12+
using namespace std;
13+
14+
class ImageBoxPanel : public Panel
15+
{
16+
public:
17+
void create()
18+
{
19+
auto style = std::make_shared<PanelStyle>();
20+
style->titleStyle->normalColor = sf::Color(60, 60, 60, 255);
21+
setWidgetStyle(style);
22+
23+
setTitle(L"瓦片编辑器");
24+
25+
auto tableLayout = std::make_shared<TableLayout>(3, 3);
26+
tableLayout->setSpacing(2.0f);
27+
28+
auto imageManager = Application::getInstance()->getComponent<ResourceManager<sf::Image>>();
29+
30+
auto tileSet = imageManager->loadFromFile("../resource/images/tileset.png");
31+
32+
textures.resize(9);
33+
34+
std::shared_ptr<ImageBoxStyle> imageBoxStyle = std::make_shared<ImageBoxStyle>();
35+
//imageBoxStyle->outlinePressedStyle = {sf::Color::Red, 1.2f};
36+
//imageBoxStyle->outlineStyle = {sf::Color::Blue, 1.2f};
37+
38+
for(uint32_t i = 0; i < 9; i++) {
39+
auto imageBox = std::make_shared<ImageBox>(sf::Vector2f(32, 32));
40+
imageBox->setWidgetStyle(imageBoxStyle);
41+
42+
sf::IntRect rect;
43+
rect.left = 0 + (i % 3) * 32;
44+
rect.top = 0 + (i / 3) * 32;
45+
rect.width = 32;
46+
rect.height = 32;
47+
48+
textures[i].loadFromImage(*tileSet, rect);
49+
imageBox->setTexture(textures[i]);
50+
tableLayout->addWidget(imageBox, i / 3, i % 3);
51+
}
52+
setContextWidget(tableLayout);
53+
}
54+
private:
55+
std::vector<sf::Texture> textures;
56+
};
57+
58+
int main()
59+
{
60+
auto size = sf::Vector2f(960, 720);
61+
auto setting = sf::ContextSettings();
62+
setting.antialiasingLevel = 12;
63+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-31",
64+
sf::Style::Close, setting);
65+
window->setVerticalSyncEnabled(true);
66+
67+
auto app = Application::getInstance();
68+
app->setWindow(window);
69+
70+
auto scene = std::make_shared<Scene>();
71+
auto sceneManager = std::make_shared<SceneManager>();
72+
sceneManager->addScene(scene);
73+
sceneManager->setInitialScene(scene);
74+
75+
auto desktop = std::make_shared<Desktop>();
76+
scene->addComponent(desktop);
77+
78+
auto panel = std::make_shared<ImageBoxPanel>();
79+
panel->create();
80+
81+
desktop->addWidget(panel, HMode_Center, VMode_Center);
82+
app->execute(sceneManager);
83+
return 0;
84+
}

application/chapter-32/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_executable(chapter-32 main.cpp)
2+
target_link_libraries(chapter-32 framework sfml-audio sfml-system sfml-graphics sfml-window)

0 commit comments

Comments
 (0)