Commit e5077c89 authored by louiz’'s avatar louiz’

SelectionPanel, add missing files. (including 2 images) #93

parent e5b142a7
......@@ -117,12 +117,22 @@ add_library(mpreal STATIC ${source_mpreal})
target_link_libraries(
mpreal
serialization
${MPFR_LIBRARIES}
${GMP_LIBRARIES}
${Boost_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
fixmath
)
file(
GLOB
source_fixmath
src/fixmath/*.c
src/fixmath/*.cpp
)
add_library(fixmath STATIC ${source_fixmath})
target_link_libraries(
fixmath
logging
)
file(
GLOB
......@@ -232,6 +242,7 @@ add_library(serialization STATIC ${source_serialization})
target_Link_libraries(
serialization
logging
world
${Boost_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)
......
......@@ -54,10 +54,3 @@ License: zlib
Website: http://www.adp-gmbh.ch/cpp/common/base64.html
Sources: https://github.com/ReneNyffenegger/development_misc/tree/master/base64
-----------------------------------------------
------------------ MPFR C++ -------------------
License: LGPL
Website: http://www.holoborodko.com/pavel/mpfr/
-----------------------------------------------
......@@ -13,12 +13,10 @@
# define __EVENT_HPP__
#include <serialization/serializable.hpp>
#include <world/serializable_entity.hpp>
#include <world/entity.hpp>
#include <network/command.hpp>
#include <mpreal/mpreal.h>
using namespace mpfr;
class Event: public Serializable
{
public:
......@@ -116,7 +114,7 @@ private:
class EntityEvent: public ActionEvent
{
public:
EntityEvent(SerializableEntity* entity):
EntityEvent(Entity* entity):
ActionEvent("NEW_ENTITY"),
entity(entity)
{}
......@@ -132,7 +130,7 @@ public:
ActionEvent::serialize(ar, v);
ar & entity;
}
SerializableEntity* entity;
Entity* entity;
private:
EntityEvent(const EntityEvent&);
......
......@@ -238,8 +238,8 @@ void Camera::draw(const Screen* screen)
uint end_y = start_y + win_size.y / TILE_HEIGHT + 2;
uint start_x = static_cast<uint>(this->x) / TILE_WIDTH;
uint end_x = start_x + win_size.x / TILE_WIDTH + 2;
int cellx;
int celly;
short cellx;
short celly;
this->world->sort_entities();
GraphTile* tile;
......@@ -359,8 +359,8 @@ void Camera::fixup_camera_position()
sf::Vector2u Camera::world_to_camera_position(const Position& pos) const
{
sf::Vector2u res;
res.x = ((pos.x * TILE_WIDTH) / CELL_SIZE).toLong();
res.y = ((pos.y * TILE_HEIGHT) / CELL_SIZE).toLong();
res.x = (pos.x.toLong() * TILE_WIDTH) / CELL_SIZE;
res.y = (pos.y.toLong() * TILE_HEIGHT) / CELL_SIZE;
mpreal height = this->world->get_position_height(pos) * 32;
res.y -= height.toLong();
return res;
......@@ -376,13 +376,14 @@ Position Camera::camera_to_world_position(const int x,
uint offset = (cell_size - (res.y.toLong() % cell_size));
uint i = 0;
mpreal height_of_bottom_cell = this->world->get_position_height(Position(res.x, res.y + offset));
assert(offset <= 32767);
mpreal height_of_bottom_cell = this->world->get_position_height(Position(res.x, res.y + static_cast<short>(offset)));
if (height_of_bottom_cell > ((offset) * (1.f/LAYER_HEIGHT)))
res.y += (height_of_bottom_cell * LAYER_HEIGHT).toLong();
else
{
offset += cell_size;
height_of_bottom_cell = this->world->get_position_height(Position(res.x, res.y + offset));
height_of_bottom_cell = this->world->get_position_height(Position(res.x, res.y + static_cast<short>(offset)));
if (height_of_bottom_cell > (offset * (1.f/LAYER_HEIGHT)))
res.y += (height_of_bottom_cell * LAYER_HEIGHT).toLong();
else
......
......@@ -24,8 +24,6 @@
#include <mpreal/mpreal.h>
#include <world/position.hpp>
using namespace mpfr;
class Minimap;
class Screen;
......
#include <gui/hud/selection_panel.hpp>
#include <world/client_world.hpp>
SelectionPanel::SelectionPanel(sf::RenderWindow* win, const Selection* selection):
current_tab(0),
win(win),
selection(selection)
{
}
SelectionPanel::~SelectionPanel()
{
}
void SelectionPanel::draw()
{
// Check if we are not on a tab that is empty because of the selection
// being too small
const std::size_t square_per_tab = SQUARES_PER_LINE * LINES_PER_TAB;
if (this->current_tab * square_per_tab >= this->selection->size())
this->current_tab = 0;
const std::list<const Entity*> entities = this->selection->get_entities();
std::list<const Entity*>::const_iterator it;
sf::Texture texture;
texture.loadFromFile("data/images/unit50.png");
sf::Sprite sprite;
sprite.setTexture(texture);
uint x = PANEL_X;
uint y = PANEL_Y;
int tab = -1;
std::size_t n = 0;
for (it = entities.begin(); it != entities.end(); ++it)
{
if (n % (SQUARES_PER_LINE * LINES_PER_TAB) == 0)
{
tab++;
this->draw_tab(tab);
}
n++;
if (tab == this->current_tab)
{
sprite.setPosition(x, win->getSize().y - y);
win->draw(sprite);
if (n % SQUARES_PER_LINE == 0)
{
x = PANEL_X;
y -= SQUARE_SIZE + SPACE_BETWEEN_LINES;
}
else
x += SQUARE_SIZE + SPACE_BETWEEN_SQUARES;
}
}
}
void SelectionPanel::draw_tab(const std::size_t tab_number)
{
const std::size_t n = tab_number+1;
sf::RectangleShape tab(sf::Vector2f(TAB_WIDTH, TAB_HEIGHT));
tab.setOutlineColor(sf::Color::Black);
if (this->current_tab == tab_number)
tab.setFillColor(sf::Color(120, 120, 12));
else
tab.setFillColor(sf::Color(120, 120, 120));
tab.setOutlineThickness(1);
tab.setPosition(TABS_X, win->getSize().y - TABS_Y + tab_number * (TAB_HEIGHT + SPACE_BETWEEN_TABS));
win->draw(tab);
}
bool SelectionPanel::handle_event(sf::RenderWindow* win, const sf::Event& event, ClientWorld* world)
{
// Check if we clicked on a tab
if ((event.mouseButton.x < (TABS_X + TAB_WIDTH)) &&
(event.mouseButton.x > (TABS_X)))
{
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
{
const int y = event.mouseButton.y - (win->getSize().y - TABS_Y);
if (y >= 0)
{
int n = y / (TAB_HEIGHT + SPACE_BETWEEN_TABS);
// Check if the tab we try to select does exist.
const std::size_t square_per_tab = SQUARES_PER_LINE * LINES_PER_TAB;
if (this->current_tab * square_per_tab >= this->selection->size())
return false;
if (y % (TAB_HEIGHT + SPACE_BETWEEN_TABS) <= TAB_HEIGHT)
{
this->current_tab = n;
return true;
}
}
}
}
// Check if we are over the entity squares
if (event.type == sf::Event::MouseButtonPressed)
if ((event.mouseButton.x > PANEL_X) &&
(event.mouseButton.x < (PANEL_X + (SPACE_BETWEEN_SQUARES + SQUARE_SIZE) * SQUARES_PER_LINE - SPACE_BETWEEN_SQUARES)) &&
(event.mouseButton.y > win->getSize().y - PANEL_Y))
{
log_error("event on panel");
const Entity* entity = this->get_entity_under_mouse();
if (!entity)
return false;
switch (event.mouseButton.button)
{
case sf::Mouse::Left:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::RShift) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
{
log_error(this->selection->size());
world->unselect_entity(entity);
log_error(this->selection->size());
}
else
{
world->clear_selection();
world->select_entity(entity);
}
break;
}
return true;
}
return false;
}
const Entity* SelectionPanel::get_entity_under_mouse() const
{
const sf::Vector2i pos = sf::Mouse::getPosition(*this->win);
const int posx = pos.x - PANEL_X;
const int posy = pos.y - (this->win->getSize().y - PANEL_Y);
const int x = posx / (SQUARE_SIZE + SPACE_BETWEEN_SQUARES);
const int y = posy / (SQUARE_SIZE + SPACE_BETWEEN_LINES);
int n = (this->current_tab * (SQUARES_PER_LINE * LINES_PER_TAB)) +
y * SQUARES_PER_LINE + x;
log_error(n);
const std::list<const Entity*> entities = this->selection->get_entities();
std::list<const Entity*>::const_iterator it;
int nn = 0;
for (it = entities.begin(); it != entities.end(); ++it)
{
if (nn == n)
return *it;
++nn;
}
return 0;
}
bool SelectionPanel::is_entity_hovered(const Entity* entity) const
{
const std::list<const Entity*> entities = this->selection->get_entities();
std::list<const Entity*>::const_iterator it;
int tab = -1;
std::size_t n = 0;
for (it = entities.begin(); it != entities.end(); ++it)
{
if (n % (SQUARES_PER_LINE * LINES_PER_TAB) == 0)
tab++;
if (tab == this->current_tab && *it == entity)
{
n = n - (tab * (SQUARES_PER_LINE * LINES_PER_TAB));
const sf::Vector2i pos = sf::Mouse::getPosition(*this->win);
const std::size_t x = n % SQUARES_PER_LINE;
const std::size_t y = n / SQUARES_PER_LINE;
const int posx = pos.x - PANEL_X;
const int posy = pos.y - (this->win->getSize().y - PANEL_Y);
if (((posx >= (SQUARE_SIZE + SPACE_BETWEEN_SQUARES) * x) && (posx < (SQUARE_SIZE + SPACE_BETWEEN_SQUARES) * x + SQUARE_SIZE)) && ((posy >= (SQUARE_SIZE + SPACE_BETWEEN_TABS) * y) && (posy < (SQUARE_SIZE + SPACE_BETWEEN_TABS) * y + SQUARE_SIZE)))
return true;
}
n++;
}
return false;
}
void SelectionPanel::update()
{
}
/** @addtogroup Gui
* @{
*/
/**
TODO update this comment once I have decided on a design.
* Contains all details about how the list of entities in the current
* selection is displayed in the hud. It keeps the list internally and is
* updated whenever the world::current_selection changes. User events should
* be passed for it to to handle clicks on unit-squares, hovers, tab
* changes, etc.
*
* @class MouseSelection
*/
#ifndef __SELECTION_PANEL_HPP__
# define __SELECTION_PANEL_HPP__
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <world/selection.hpp>
#include <world/client_world.hpp>
#define SQUARE_SIZE 50
#define SQUARES_PER_LINE 10
#define SPACE_BETWEEN_SQUARES 8
#define SPACE_BETWEEN_LINES 8
#define LINES_PER_TAB 3
#define PANEL_X 650
#define PANEL_Y 190
#define TABS_X 622
#define TABS_Y 180
#define TAB_WIDTH 25
#define TAB_HEIGHT 15
#define SPACE_BETWEEN_TABS 5
class SelectionPanel
{
public:
SelectionPanel(sf::RenderWindow*, const Selection*);
~SelectionPanel();
void draw();
void draw_tab(const std::size_t);
bool handle_event(sf::RenderWindow*, const sf::Event&, ClientWorld*);
bool is_entity_hovered(const Entity*) const;
const Entity* get_entity_under_mouse() const;
void update();
private:
SelectionPanel(const SelectionPanel&);
SelectionPanel& operator=(const SelectionPanel&);
std::size_t current_tab;
sf::RenderWindow* win;
/**
* A pointer to the Selection object contained in the ClientWorld object.
*/
const Selection* const selection;
};
#endif // __SELECTION_PANEL_HPP__
/*
Multi-precision real number class. C++ interface fo MPFR library.
Project homepage: http://www.holoborodko.com/pavel/
Contact e-mail: pavel@holoborodko.com
Copyright (c) 2008-2011 Pavel Holoborodko
Core Developers:
Pavel Holoborodko, Dmitriy Gubanov, Konstantin Holoborodko.
Contributors:
Brian Gladman, Helmut Jarausch, Fokko Beekhof, Ulrich Mutze,
Heinz van Saanen, Pere Constans, Peter van Hoof, Gael Guennebaud,
Tsai Chia Cheng, Alexei Zubanov.
****************************************************************************
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
****************************************************************************
****************************************************************************
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include <cstring>
#include "mpreal.h"
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
#include "dlmalloc.h"
#endif
using std::ws;
using std::cerr;
using std::endl;
using std::string;
using std::ostream;
using std::istream;
namespace mpfr{
mp_rnd_t mpreal::default_rnd = GMP_RNDN; //(mpfr_get_default_rounding_mode)();
mp_prec_t mpreal::default_prec = 64; //(mpfr_get_default_prec)();
int mpreal::default_base = 10;
int mpreal::double_bits = -1;
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
bool mpreal::is_custom_malloc = false;
#endif
// Default constructor: creates mp number and initializes it to 0.
mpreal::mpreal()
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,default_prec);
mpfr_set_ui(mp,0,default_rnd);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const mpreal& u)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,mpfr_get_prec(u.mp));
mpfr_set(mp,u.mp,default_rnd);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const mpfr_t u)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,mpfr_get_prec(u));
mpfr_set(mp,u,default_rnd);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const mpf_t u)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,(mp_prec_t) mpf_get_prec(u)); // (gmp: mp_bitcnt_t) unsigned long -> long (mpfr: mp_prec_t)
mpfr_set_f(mp,u,default_rnd);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const mpz_t u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,prec);
mpfr_set_z(mp,u,mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const mpq_t u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,prec);
mpfr_set_q(mp,u,mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const double u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
if(double_bits == -1 || fits_in_bits(u, double_bits))
{
mpfr_init2(mp,prec);
mpfr_set_d(mp,u,mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
else
throw conversion_overflow();
}
mpreal::mpreal(const long double u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,prec);
mpfr_set_ld(mp,u,mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const unsigned long int u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,prec);
mpfr_set_ui(mp,u,mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const unsigned int u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,prec);
mpfr_set_ui(mp,u,mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const long int u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,prec);
mpfr_set_si(mp,u,mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const int u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,prec);
mpfr_set_si(mp,u,mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
#if defined (MPREAL_HAVE_INT64_SUPPORT)
mpreal::mpreal(const uint64_t u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif
mpfr_init2(mp,prec);
mpfr_set_uj(mp, u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
mpreal::mpreal(const int64_t u, mp_prec_t prec, mp_rnd_t mode)
{
#if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
set_custom_malloc();
#endif