~cypheon/elfelli

ref: d62f0945c78677b2fe75fe2a71484afb4875f9a2 elfelli/src/SimulationCanvas.h -rw-r--r-- 3.4 KiB
d62f0945 — Johann Rudloff Try enabling Travis macOS build env. 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// -*- C++ -*-
/*
 * SimulationCanvas.h
 * Copyright (C) 2006  Johann Rudloff
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef _SIMULATIONCANVAS_H_
#define _SIMULATIONCANVAS_H_

#include <vector>

#include "Simulation.h"
#include "Canvas.h"

namespace Elfelli
{

inline float sign(float f)
{
  if(f<0) return -1;
  return 1;
}

enum BodyState
  {
    BODY_STATE_NORMAL = 0,
    BODY_STATE_HIGHLIGHT,
    BODY_STATE_SELECTED,
    BODY_STATES_NUM
  };

enum DragState
  {
    DRAG_STATE_NONE = 0,
    DRAG_STATE_BODY,
    DRAG_STATE_PLATE,
    DRAG_STATE_PLATE_A,
    DRAG_STATE_PLATE_B
  };

class SimulationCanvas : public Simulation, public Canvas
{
public:
  SimulationCanvas();
  ~SimulationCanvas();

  void operator=(const Simulation& sim);

  bool has_selection();

  void refresh();
  void clear();
  bool delete_body(unsigned int n);
  bool delete_plate(unsigned int n);
  bool delete_selected();

  bool set_selected_charge(float value);
  float get_selected_charge();

  bool change_selected_charge(float delta);
  bool increase_selected_charge(bool small=false);
  bool decrease_selected_charge(bool small=false);

  sigc::signal<void> signal_selected_charge_changed();
  sigc::signal<void> signal_selection_changed();

  static const float MAX_CHARGE;
  static const float MIN_CHARGE;
  static const float CHARGE_STEP;
  static const float CHARGE_STEP_SMALL;

private:
  void draw_flux_lines();
  void draw_bodies(bool draw_selected=true);
  inline void draw_body(int n);
  void draw_plates(bool draw_selected=true);
  inline void draw_plate(int n);

  bool point_hits_body(Body& b, int x, int y);
  bool point_hits_plate_a(PlateBody& p, int x, int y);
  bool point_hits_plate_b(PlateBody& p, int x, int y);
  bool point_hits_plate(PlateBody& p, int x, int y);
  int object_at(int x, int y);

  static const char *color_names[];

  int body_radius, plate_radius;

  DragState drag_state; 
  int active;

  bool mouse_pressed;
  int mouse_over;
  Gdk::Point last_click, drag_offset;

  Glib::RefPtr<Gdk::GC> gc, gc_black, gc_white, gc_selection, gc_platebody;
  Gdk::Color colors[BODY_STATES_NUM * 2];
  Glib::RefPtr<Gdk::Pixmap> lines_pixmap;
  std::vector<Path> paths;

  sigc::signal<void> sig_selected_charge_changed;
  sigc::signal<void> sig_selection_changed;

protected:
  virtual void run();
  void plot();

  virtual void after_realize_event();
  virtual bool on_configure_event(GdkEventConfigure *event);

  virtual bool on_motion_notify_event(GdkEventMotion *event);
  virtual bool on_button_press_event(GdkEventButton *event);
  virtual bool on_button_release_event(GdkEventButton *event);
  virtual bool on_scroll_event(GdkEventScroll *event);

  virtual bool on_key_press_event(GdkEventKey *event);
};

}

#endif // _SIMULATIONCANVAS_H_