source: branches/species/NTREE/NT_shadeTree.cxx

Last change on this file was 19613, checked in by westram, 2 months ago
  • reintegrates 'lib' into 'trunk'
    • replace dynamic library AWT by several static libraries: APP, ARB_SPEC, MASKS, CANVAS, MAPKEY, GUI_TK
    • now also check wrong library dependencies for untested units (only4me)
  • adds: log:branches/lib@19578:19612
File size: 5.1 KB
Line 
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
25typedef SmartPtr<TreePositionLookup> TreePositionLookupPtr;
26
27class TopologyShader: public ShaderPlugin {
28    RefPtr<AWT_graphic_tree> agt;
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
37public:
38    TopologyShader(AWT_graphic_tree *agt_) :
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    }
73    static void tree_changed_cb(AWT_graphic_tree *IF_ASSERTION_USED(by), TopologyShader *shader) {
74        nt_assert(shader->is_linked_with(by));
75        shader->tree_changed_cb();
76    }
77};
78
79void 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);
85        agt->install_tree_changed_callback(gtcb);
86    }
87    else {
88        agt->uninstall_tree_changed_callback();
89    }
90}
91
92// -----------------------
93//      NT_TreeShader
94
95class 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
102        AW_root::SINGLETON->awar(AWAR_TREE_RECOMPUTE)->touch();
103    }
104
105public:
106    NT_TreeShader(TREE_canvas *ntw, GBDATA *gb_main) :
107        shader(registerItemShader(ntw->awr,
108                                  ntw->gc_manager,
109                                  BoundItemSel(gb_main, SPECIES_get_selector()),
110                                  "tree",
111                                  "Tree shading",
112                                  "tree_shading.hlp",
113                                  NT_TreeShader::reshade,
114                                  AWT_GC_NONE_MARKED))
115    {
116        AWT_graphic_tree *agt         = DOWNCAST(AWT_graphic_tree*, ntw->gfx);
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();
125        colorize_groups = shader->overlay_color_groups();
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 {
136        shader->popup_config_window(AW_root::SINGLETON);
137    }
138};
139
140// ----------------------------
141//      external interface
142
143void NT_install_treeShader(TREE_canvas *ntw, GBDATA *gb_main) {
144    AP_tree::set_tree_shader(new NT_TreeShader(ntw, gb_main));
145}
146
147void NT_configure_treeShader() {
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
Note: See TracBrowser for help on using the repository browser.