Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Печенин Данила Михайлович
Pacman
Commits
84c30d32
Commit
84c30d32
authored
3 weeks ago
by
Печенин Данила Михайлович
Browse files
Options
Download
Patches
Plain Diff
All buttons in SelectState work. They close app.
parent
0a84ad8c
main
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
source/BasicAbstractions/Command.h
+2
-2
source/BasicAbstractions/Command.h
source/States/ChangeStateCommand.h
+16
-0
source/States/ChangeStateCommand.h
source/States/ExitState/ExitState.h
+2
-7
source/States/ExitState/ExitState.h
source/States/SelectState/SelectState.cpp
+31
-18
source/States/SelectState/SelectState.cpp
source/States/SelectState/SelectState.h
+4
-4
source/States/SelectState/SelectState.h
with
55 additions
and
31 deletions
+55
-31
source/BasicAbstractions/Command.h
+
2
−
2
View file @
84c30d32
#pragma once
struct
ISelectCommand
{
virtual
void
execute
()
=
0
;
virtual
~
ISelectCommand
()
=
default
;
virtual
void
execute
()
=
0
;
virtual
~
ISelectCommand
()
=
default
;
};
\ No newline at end of file
This diff is collapsed.
Click to expand it.
source/States/ChangeStateCommand.h
0 → 100644
+
16
−
0
View file @
84c30d32
#pragma once
#include
"../BasicAbstractions/Command.h"
#include
"../Application/IState.h"
#include
"ExitState/ExitState.h"
struct
ChangeStateCommand
:
ISelectCommand
{
explicit
ChangeStateCommand
(
IStateManager
&
state_manager
)
:
m_state_manager
(
state_manager
)
{}
protected
:
IStateManager
&
m_state_manager
;
};
struct
ExitCommand
:
ChangeStateCommand
{
using
ChangeStateCommand
::
ChangeStateCommand
;
void
execute
()
override
{
m_state_manager
.
set_next_state
(
std
::
make_unique
<
ExitState
>
(
m_state_manager
));
}
};
This diff is collapsed.
Click to expand it.
source/States/ExitState/ExitState.h
+
2
−
7
View file @
84c30d32
#pragma once
#include
<iostream>
/// @todo remove it
#include
"../../Application/IState.h"
#include
"../../Application/IWindowKeeper.h"
struct
ExitState
:
IState
{
explicit
ExitState
(
IStateManager
&
state_manager
)
:
IState
(
state_manager
)
{}
bool
do_step
()
override
{
std
::
cout
<<
"Exiting..."
<<
std
::
endl
;
return
false
;
}
using
IState
::
IState
;
bool
do_step
()
override
{
return
false
;
}
};
\ No newline at end of file
This diff is collapsed.
Click to expand it.
source/States/SelectState/SelectState.cpp
+
31
−
18
View file @
84c30d32
#include
"SelectState.h"
#include
"../
ExitState/ExitState
.h"
#include
"../
ChangeStateCommand
.h"
#include
"../../Configuration.h"
#include
"../../BasicAbstractions/Font.h"
void
Button
::
draw_into
(
sf
::
RenderWindow
&
window
)
const
{
window
.
draw
(
m_rectangle
);
window
.
draw
(
m_text
);
}
void
Button
::
set
(
const
sf
::
Vector2f
pos
,
const
sf
::
Vector2f
size
,
const
std
::
string
&
text
,
const
size_t
font_size
)
{
//, std::shared_ptr<ISelectCommand> ptr_command) {
void
Button
::
set
(
const
sf
::
Vector2f
pos
,
const
sf
::
Vector2f
size
,
const
std
::
string
&
text
,
const
size_t
font_size
,
std
::
unique_ptr
<
ISelectCommand
>
ptr_command
)
{
m_rectangle
.
setSize
(
size
);
m_rectangle
.
setPosition
(
pos
);
m_rectangle
.
setFillColor
(
config
::
BUTTON_COLOR_FILL
);
...
...
@@ -25,6 +20,7 @@ void Button::set(const sf::Vector2f pos, const sf::Vector2f size, const std::str
round
(
button_pos
.
x
+
(
button_size
.
x
-
text_bounds
.
width
)
/
2
-
text_bounds
.
left
),
round
(
button_pos
.
y
+
(
button_size
.
y
-
text_bounds
.
height
)
/
2
-
text_bounds
.
top
)
);
m_ptr_command
=
std
::
move
(
ptr_command
);
}
void
Button
::
select
()
{
...
...
@@ -40,26 +36,41 @@ bool Button::is_position_in(const sf::Vector2f pos) const {
&&
pos
.
y
>=
m_rectangle
.
getPosition
().
y
&&
pos
.
y
<=
(
m_rectangle
.
getPosition
().
y
+
m_rectangle
.
getSize
().
y
);
}
void
Button
::
push
()
const
{
m_ptr_command
->
execute
();
}
void
Button
::
draw_into
(
sf
::
RenderWindow
&
window
)
const
{
window
.
draw
(
m_rectangle
);
window
.
draw
(
m_text
);
}
Menu
::
Menu
(
IStateManager
&
state_manager
)
{
const
float
pos_left
=
(
static_cast
<
float
>
(
config
::
SELECT_LEVEL_VIDEO_MODE
.
width
)
-
config
::
BUTTON_SIZE
.
x
)
/
2
;
const
float
pos_diff
=
(
static_cast
<
float
>
(
config
::
SELECT_LEVEL_VIDEO_MODE
.
height
)
-
config
::
BUTTON_SIZE
.
y
*
4
)
/
10
;
const
float
pos_top
=
(
static_cast
<
float
>
(
config
::
SELECT_LEVEL_VIDEO_MODE
.
height
)
-
config
::
BUTTON_SIZE
.
y
*
4
-
pos_diff
*
3
)
/
2
;
m_buttons
[
0
].
set
(
sf
::
Vector2f
{
pos_left
,
pos_top
},
config
::
BUTTON_SIZE
,
config
::
BUTTON_TEXT_EASY
,
config
::
BUTTON_FONT_SIZE
);
m_buttons
[
1
].
set
(
sf
::
Vector2f
{
pos_left
,
pos_top
+
config
::
BUTTON_SIZE
.
y
+
pos_diff
},
config
::
BUTTON_SIZE
,
config
::
BUTTON_TEXT_MEDIUM
,
config
::
BUTTON_FONT_SIZE
);
m_buttons
[
2
].
set
(
sf
::
Vector2f
{
pos_left
,
pos_top
+
2
*
(
config
::
BUTTON_SIZE
.
y
+
pos_diff
)},
config
::
BUTTON_SIZE
,
config
::
BUTTON_TEXT_HARD
,
config
::
BUTTON_FONT_SIZE
);
m_buttons
[
3
].
set
(
sf
::
Vector2f
{
pos_left
,
pos_top
+
3
*
(
config
::
BUTTON_SIZE
.
y
+
pos_diff
)},
config
::
BUTTON_SIZE
,
config
::
BUTTON_TEXT_EXIT
,
config
::
BUTTON_FONT_SIZE
);
m_buttons
[
0
].
set
(
sf
::
Vector2f
{
pos_left
,
pos_top
},
config
::
BUTTON_SIZE
,
config
::
BUTTON_TEXT_EASY
,
config
::
BUTTON_
FONT_
SIZE
,
std
::
make_unique
<
ExitCommand
>
(
state_manager
)
);
m_buttons
[
1
].
set
(
sf
::
Vector2f
{
pos_left
,
pos_top
+
config
::
BUTTON_SIZE
.
y
+
pos_diff
},
config
::
BUTTON_SIZE
,
config
::
BUTTON_TEXT_MEDIUM
,
config
::
BUTTON_FONT_SIZE
,
std
::
make_unique
<
ExitCommand
>
(
state_manager
)
);
m_buttons
[
2
].
set
(
sf
::
Vector2f
{
pos_left
,
pos_top
+
2
*
(
config
::
BUTTON_SIZE
.
y
+
pos_diff
)},
config
::
BUTTON_SIZE
,
config
::
BUTTON_TEXT_HARD
,
config
::
BUTTON_FONT_SIZE
,
std
::
make_unique
<
ExitCommand
>
(
state_manager
)
);
m_buttons
[
3
].
set
(
sf
::
Vector2f
{
pos_left
,
pos_top
+
3
*
(
config
::
BUTTON_SIZE
.
y
+
pos_diff
)},
config
::
BUTTON_SIZE
,
config
::
BUTTON_TEXT_EXIT
,
config
::
BUTTON_FONT_SIZE
,
std
::
make_unique
<
ExitCommand
>
(
state_manager
)
);
}
void
Menu
::
process_mouse
(
const
sf
::
Vector2f
pos
,
bool
is_pressed
)
{
bool
Menu
::
process_mouse
(
const
sf
::
Vector2f
pos
,
const
bool
is_pressed
)
{
for
(
auto
&
button
:
m_buttons
)
{
if
(
button
.
is_position_in
(
pos
))
if
(
button
.
is_position_in
(
pos
))
{
button
.
select
();
if
(
is_pressed
)
{
button
.
push
();
return
false
;
}
}
else
button
.
unselect
();
}
return
true
;
}
void
Menu
::
draw_into
(
sf
::
RenderWindow
&
window
)
const
{
...
...
@@ -79,7 +90,9 @@ void SelectState::event_handling() {
}
void
SelectState
::
update
()
{
m_menu
.
process_mouse
(
m_window
.
mapPixelToCoords
(
sf
::
Mouse
::
getPosition
(
m_window
)),
true
);
if
(
!
m_menu
.
process_mouse
(
m_window
.
mapPixelToCoords
(
sf
::
Mouse
::
getPosition
(
m_window
)),
sf
::
Mouse
::
isButtonPressed
(
sf
::
Mouse
::
Left
)))
m_window
.
close
();
}
void
SelectState
::
render
()
{
...
...
This diff is collapsed.
Click to expand it.
source/States/SelectState/SelectState.h
+
4
−
4
View file @
84c30d32
...
...
@@ -7,22 +7,22 @@
class
Button
:
public
IDrawable
{
public:
Button
()
=
default
;
void
set
(
sf
::
Vector2f
pos
,
sf
::
Vector2f
size
,
const
std
::
string
&
text
,
size_t
font_size
);
//
, std::
shared
_ptr<ISelectCommand> ptr_command);
void
set
(
sf
::
Vector2f
pos
,
sf
::
Vector2f
size
,
const
std
::
string
&
text
,
size_t
font_size
,
std
::
unique
_ptr
<
ISelectCommand
>
ptr_command
);
void
select
();
void
unselect
();
bool
is_position_in
(
sf
::
Vector2f
pos
)
const
;
//
void push();
void
push
()
const
;
void
draw_into
(
sf
::
RenderWindow
&
window
)
const
override
;
private:
sf
::
Text
m_text
;
sf
::
RectangleShape
m_rectangle
;
//
std::
shared
_ptr<ISelectCommand> m_ptr_command;
/// @todo shared_ptr?
std
::
unique
_ptr
<
ISelectCommand
>
m_ptr_command
;
};
class
Menu
:
public
IDrawable
{
public:
explicit
Menu
(
IStateManager
&
state_manager
);
void
process_mouse
(
sf
::
Vector2f
pos
,
bool
is_pressed
);
bool
process_mouse
(
sf
::
Vector2f
pos
,
bool
is_pressed
);
void
draw_into
(
sf
::
RenderWindow
&
window
)
const
override
;
private:
std
::
array
<
Button
,
4
>
m_buttons
;
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets