Skip to content

Commit

Permalink
Camera sensitivity (#23)
Browse files Browse the repository at this point in the history
Add a camera rotation sensitivity slider to the main tool window - this will probably be moved to a settings window at some point. The slider is also currently unlabeled. The sensitivity currently scales the sensitivity between dividing by 200 (low) and by 25 (high).

Also fixed an issue where alpha blending wasn't working - it was cutting holes through the render target stack all the way down to the clear colour.

Controls can now handle input. The input code needs to move out to its own classes though, it shouldn't be in the controls themselves as there is currently some up and down movement for notifying about which control has the focus etc.

#20
  • Loading branch information
chreden authored Feb 12, 2018
1 parent 470cb32 commit 39f53be
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 32 deletions.
5 changes: 2 additions & 3 deletions trview.ui.render/RenderNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ namespace trview
child->render(context, sprite);
}

render_self(context, sprite);

RenderTargetStore render_target_store(context);
render_self(context, sprite);
context->OMSetRenderTargets(1, &_render_target_view.p, nullptr);
for (auto& child : _child_nodes)
{
if (!child->visible())
Expand All @@ -76,7 +76,6 @@ namespace trview
// Render the child in the correct position on the render target.
auto pos = child->position();
auto size = child->size();
context->OMSetRenderTargets(1, &_render_target_view.p, nullptr);
sprite.render(context, child->node_texture_view(), pos.x, pos.y, size.width, size.height);
}
}
Expand Down
3 changes: 2 additions & 1 deletion trview.ui/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ namespace trview
set_texture(_up_image);
}

void Button::clicked()
bool Button::clicked(Point position)
{
set_state(!_state);
on_click.raise();
return true;
}

void Button::set_state(bool state)
Expand Down
2 changes: 1 addition & 1 deletion trview.ui/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace trview

void set_state(bool state);
protected:
virtual void clicked() override;
virtual bool clicked(Point position) override;
private:
Texture _up_image;
Texture _down_image;
Expand Down
84 changes: 81 additions & 3 deletions trview.ui/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ namespace trview
return _position;
}

void Control::set_position(Point position)
{
_position = position;
}

Size Control::size() const
{
return _size;
Expand Down Expand Up @@ -71,16 +76,89 @@ namespace trview

// If none of the child elements have handled this event themselves, call the
// clicked function of this control.
if (!handled)
if (handled)
{
return true;
}

// Promote controls to focus control, or clear if there are no controls that
// accepted the event.
bool handled_by_self = clicked(position);
if (handled_by_self)
{
set_focus_control(this);
}
else if (!_parent)
{
set_focus_control(nullptr);
}
return handled_by_self;
}

bool Control::mouse_move(Point position)
{
if (_focus_control && _focus_control != this)
{
bool focus_handled = _focus_control->mouse_move(position);
if (focus_handled)
{
return true;
}
}

// Bounds check - before child elements are checked.
if (!(position.x >= 0 && position.y >= 0 && position.x <= _size.width && position.y <= _size.height))
{
return false;
}

bool handled = false;
for (auto& child : _child_elements)
{
clicked();
// Convert the position into the coordinate space of the child element.
handled |= child->mouse_move(position - child->position());
}

// If none of the child elements have handled this event themselves, call the
// move function of this control.
return handled | move(position);
}

bool Control::mouse_up(Point position)
{
set_focus_control(nullptr);
return true;
}

void Control::clicked()
bool Control::clicked(Point position)
{
return false;
}

bool Control::move(Point position)
{
return false;
}

void Control::set_focus_control(Control* control)
{
if (_parent)
{
_parent->set_focus_control(control);
}
else
{
_focus_control = control;
}
}

Control* Control::focus_control() const
{
if (_parent)
{
return _parent->focus_control();
}
return _focus_control;
}
}
}
24 changes: 22 additions & 2 deletions trview.ui/Control.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ namespace trview
virtual ~Control() = 0;

// Get the X and Y position of the control relative to the parent window.
// To change the position of the control, call set_position.
Point position() const;

// Set the X and Y position of the control relative to the parent window.
// To get the position of the control, call position.
void set_position(Point position);

// Get the width and height of the control.
Size size() const;

Expand Down Expand Up @@ -48,15 +53,30 @@ namespace trview
std::vector<Control*> child_elements() const;

// Process a mouse_down event at the position specified.
// Returns whether the mouse click was within the bounds of the control element.
// Returns whether the mouse click was within the bounds of the control element and was handled.
bool mouse_down(Point position);

bool mouse_up(Point position);

// Process a mouse_move event at the position specified.
// Returns whether the mouse move was handled.
bool mouse_move(Point position);
protected:
// To be called when the user interface element has been clicked.
// This should be overriden by child elements to handle a click.
virtual void clicked();
virtual bool clicked(Point position);

// To be called when the mouse was moved over the element.
// This should be overriden by child elements to handle a move.
virtual bool move(Point position);

void set_focus_control(Control* control);

Control* focus_control() const;
private:
std::vector<std::unique_ptr<Control>> _child_elements;
Control* _parent;
Control* _focus_control;
Point _position;
Size _size;
bool _visible;
Expand Down
63 changes: 63 additions & 0 deletions trview.ui/Slider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "Slider.h"
#include <algorithm>

namespace trview
{
namespace ui
{
namespace
{
const float BlobWidth = 10.f;
}

Slider::Slider(Point position, Size size)
: Window(position, size, Colour(0, 0, 0, 0))
{
// Create the child windows (and store them for later manipulation).
auto line = std::make_unique<Window>(
Point(BlobWidth, size.height / 2.f - 1.f),
Size(size.width - BlobWidth * 2.f, 2.f),
Colour(1, 0, 0, 0));

auto blob = std::make_unique<Window>(
Point(0, 0),
Size(BlobWidth, size.height),
Colour(1.0f, 0.2f, 0.2f, 0.2f));

_blob = blob.get();

add_child(std::move(line));
add_child(std::move(blob));

set_blob_position(Point(0, 0));
}

bool Slider::clicked(Point position)
{
set_blob_position(position);
return true;
}

bool Slider::move(Point position)
{
if (focus_control() == this)
{
set_blob_position(position);
return true;
}
return false;
}

void Slider::set_blob_position(Point position)
{
const float SliderSize = size().width - BlobWidth * 2;
const float percentage = std::min(1.0f, std::max(0.0f, (position.x - BlobWidth) / SliderSize));
const float x = BlobWidth + percentage * SliderSize - BlobWidth * 0.5f;

const auto pos = _blob->position();
_blob->set_position(Point(x, pos.y));

on_value_changed.raise(percentage);
}
}
}
27 changes: 27 additions & 0 deletions trview.ui/Slider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "Window.h"
#include <trview.common/Event.h>

namespace trview
{
namespace ui
{
class Slider : public Window
{
public:
Slider(Point position, Size size);
virtual ~Slider() = default;

Event<float> on_value_changed;
protected:
virtual bool clicked(Point position) override;
virtual bool move(Point position) override;
private:
void set_blob_position(Point position);

float _value{ 0.5f };
ui::Control* _blob;
};
}
}
2 changes: 2 additions & 0 deletions trview.ui/trview.ui.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ClInclude Include="ParagraphAlignment.h" />
<ClInclude Include="Point.h" />
<ClInclude Include="Size.h" />
<ClInclude Include="Slider.h" />
<ClInclude Include="TextAlignment.h" />
<ClInclude Include="Window.h" />
</ItemGroup>
Expand All @@ -38,6 +39,7 @@
<ClCompile Include="Label.cpp" />
<ClCompile Include="Point.cpp" />
<ClCompile Include="Size.cpp" />
<ClCompile Include="Slider.cpp" />
<ClCompile Include="Window.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
Expand Down
6 changes: 6 additions & 0 deletions trview.ui/trview.ui.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<ClInclude Include="Button.h">
<Filter>Controls</Filter>
</ClInclude>
<ClInclude Include="Slider.h">
<Filter>Controls</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Colour.cpp">
Expand All @@ -57,6 +60,9 @@
<ClCompile Include="Button.cpp">
<Filter>Controls</Filter>
</ClCompile>
<ClCompile Include="Slider.cpp">
<Filter>Controls</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Types">
Expand Down
Loading

0 comments on commit 39f53be

Please sign in to comment.