ARB
NT_shadeTree.cxx
Go to the documentation of this file.
1 // ============================================================ //
2 // //
3 // File : NT_shadeTree.cxx //
4 // Purpose : //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in June 2016 //
7 // http://www.arb-home.de/ //
8 // //
9 // ============================================================ //
10 
11 #include "tree_position.h"
12 
13 #include <TreeDisplay.hxx>
14 #include <AP_TreeShader.hxx>
15 
16 #include <aw_root.hxx>
17 #include <aw_awar.hxx>
18 #include <aw_awar_defs.hxx>
19 
20 #define nt_assert(cond) arb_assert(cond)
21 
22 // ------------------------
23 // TopologyShader
24 
26 
29  mutable TreePositionLookupPtr pos;
30 
31  void init_specific_awars(AW_root*) OVERRIDE {}
32 
33  void update_pos_lookup() const {
34  pos = new TreePositionLookup(agt->get_logical_root());
35  }
36 
37 public:
39  ShaderPlugin("topology", "Topology shader"),
40  agt(agt_)
41  {}
42 
43  ShadedValue shade(GBDATA *gb_item) const OVERRIDE {
44  if (gb_item) {
45  if (pos.isNull()) update_pos_lookup();
46 
47  const char *name = GBT_get_name(gb_item);
48  if (name) {
49  TreeRelativePosition relpos = pos->relative(name);
50  if (relpos.is_known()) return ValueTuple::make(relpos.value());
51  }
52  }
53  return ValueTuple::undefined();
54  }
55 
56  int get_dimension() const OVERRIDE { return 1; }
57  bool customizable() const OVERRIDE { return true; }
58  void customize(AW_root *) OVERRIDE { AW_help_popup(NULp, "topology_shader.hlp"); }
59 
60  char *store_config() const OVERRIDE { return NULp; }
61  void load_or_reset_config(const char *) OVERRIDE {}
62 
63  void activate(bool on) OVERRIDE;
64 
65 #if defined(ASSERTION_USED)
66  bool is_linked_with(AWT_graphic_tree *linkedWith) { return linkedWith == agt; }
67 #endif
68 
69  void tree_changed_cb() {
70  pos.setNull();
71  trigger_reshade_if_active_cb(SIMPLE_RESHADE); // forces reshade of "other" tree-canvas
72  }
74  nt_assert(shader->is_linked_with(by));
75  shader->tree_changed_cb();
76  }
77 };
78 
79 void TopologyShader::activate(bool on) {
80  // called with true when plugin gets activated, with false when it gets deactivated
81 
82  if (on) {
83  pos.setNull(); // invalidate cached positions
84  GraphicTreeCallback gtcb = makeGraphicTreeCallback(TopologyShader::tree_changed_cb, this);
86  }
87  else {
89  }
90 }
91 
92 // -----------------------
93 // NT_TreeShader
94 
95 class NT_TreeShader: public AP_TreeShader, virtual Noncopyable {
96  ItemShader *shader; // (owned by registry in ITEM_SHADER)
97 
98  static void reshade() {
99 #if defined(DEBUG)
100  fprintf(stderr, "[NT_TreeShader::reshade] @ %zu\n", clock());
101 #endif
103  }
104 
105 public:
107  shader(registerItemShader(ntw->awr,
108  ntw->gc_manager,
110  "tree",
111  "Tree shading",
112  "tree_shading.hlp",
113  NT_TreeShader::reshade,
115  {
117  ShaderPluginPtr topo_shader = new TopologyShader(agt);
118  shader->register_plugin(topo_shader);
119  }
120  ~NT_TreeShader() OVERRIDE {}
121  void init() OVERRIDE { shader->init(); } // called by AP_tree::set_tree_shader when installed
122 
123  void update_settings() OVERRIDE {
124  colorize_marked = shader->overlay_marked();
126  shade_species = shader->active();
127  }
128 
129  ShadedValue calc_shaded_leaf_GC(GBDATA *gb_node) const OVERRIDE { return shader->shade(gb_node); }
130  ShadedValue calc_shaded_inner_GC(const ShadedValue& left, float left_ratio, const ShadedValue& right) const OVERRIDE {
131  return mix(left, left_ratio, right);
132  }
133  int to_GC(const ShadedValue& val) const OVERRIDE { return shader->to_GC(val); }
134 
135  void popup_config() const {
137  }
138 };
139 
140 // ----------------------------
141 // external interface
142 
144  AP_tree::set_tree_shader(new NT_TreeShader(ntw, gb_main));
145 }
146 
148  const AP_TreeShader *tshader = AP_tree::get_tree_shader();
149  nt_assert(tshader);
150  if (tshader) DOWNCAST(const NT_TreeShader*, tshader)->popup_config();
151 
152 }
153 
154 
155 
double value() const
Definition: tree_position.h:48
bool customizable() const OVERRIDE
void popup_config() const
AP_tree * get_logical_root()
TopologyShader(AWT_graphic_tree *agt_)
int get_dimension() const OVERRIDE
bool is_known() const
Definition: tree_position.h:47
bool active() const
Definition: item_shader.h:320
static ShadedValue make(float f)
Definition: ValueTuple.cxx:508
~NT_TreeShader() OVERRIDE
bool overlay_marked() const
Definition: item_shader.h:321
ShadedValue shade(GBDATA *gb_item) const
Definition: item_shader.h:324
void init() OVERRIDE
void customize(AW_root *) OVERRIDE
void tree_changed_cb()
#define AWAR_TREE_RECOMPUTE
bool isNull() const
test if SmartPtr is NULp
Definition: smartptr.h:248
void update_settings() OVERRIDE
void setNull()
set SmartPtr to NULp
Definition: smartptr.h:251
#define DOWNCAST(totype, expr)
Definition: downcast.h:141
NT_TreeShader(TREE_canvas *ntw, GBDATA *gb_main)
static AW_root * SINGLETON
Definition: aw_root.hxx:102
#define nt_assert(cond)
void touch()
Definition: AW_awar.cxx:207
int to_GC(const ShadedValue &val) const OVERRIDE
virtual void register_plugin(ShaderPluginPtr plugin)=0
void uninstall_tree_changed_callback()
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
void NT_install_treeShader(TREE_canvas *ntw, GBDATA *gb_main)
ShadedValue calc_shaded_leaf_GC(GBDATA *gb_node) const OVERRIDE
SmartPtr< TreePositionLookup > TreePositionLookupPtr
AWT_graphic * gfx
Definition: canvas.hxx:349
ItemShader * registerItemShader(AW_root *awr, AW_gc_manager *gcman, BoundItemSel &itemtype, const char *unique_id, const char *description, const char *help_id, ReshadeCallback reshade_cb, int undef_gc)
#define IF_ASSERTION_USED(x)
Definition: arb_assert.h:308
virtual void init()=0
ShadedValue shade(GBDATA *gb_item) const OVERRIDE
#define OVERRIDE
Definition: cxxforward.h:112
static ShadedValue undefined()
Definition: ValueTuple.cxx:505
ItemSelector & SPECIES_get_selector()
Definition: species.cxx:139
ShadedValue mix(const ShadedValue &val1, float val1_ratio, const ShadedValue &val2)
Definition: item_shader.h:174
void trigger_reshade_if_active_cb(ReshadeMode mode)
Definition: item_shader.h:361
bool is_linked_with(AWT_graphic_tree *linkedWith)
void NT_configure_treeShader()
#define NULp
Definition: cxxforward.h:116
void AW_help_popup(UNFIXED, const char *help_file)
Definition: AW_help.cxx:642
const char * GBT_get_name(GBDATA *gb_item)
Definition: aditem.cxx:468
char * store_config() const OVERRIDE
void load_or_reset_config(const char *) OVERRIDE
static const AP_TreeShader * get_tree_shader()
Definition: AP_Tree.hxx:372
void install_tree_changed_callback(const GraphicTreeCallback &gtcb)
ShadedValue calc_shaded_inner_GC(const ShadedValue &left, float left_ratio, const ShadedValue &right) const OVERRIDE
GBDATA * gb_main
Definition: adname.cxx:32
static void set_tree_shader(AP_TreeShader *new_shader)
Definition: AP_Tree.cxx:704
bool overlay_color_groups() const
Definition: item_shader.h:322
virtual void popup_config_window(AW_root *awr)=0
void activate(bool on) OVERRIDE
TreeRelativePosition relative(const char *name) const
Definition: tree_position.h:78
int to_GC(const ShadedValue &val) const
Definition: item_shader.h:328
static void tree_changed_cb(AWT_graphic_tree *by, TopologyShader *shader)