Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_buttons / buttons_texture.c
index 50791cf..5feb74e 100644 (file)
@@ -41,8 +41,6 @@
 
 #include "DNA_brush_types.h"
 #include "DNA_ID.h"
-#include "DNA_lamp_types.h"
-#include "DNA_material_types.h"
 #include "DNA_node_types.h"
 #include "DNA_object_types.h"
 #include "DNA_object_force_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
 #include "DNA_space_types.h"
-#include "DNA_world_types.h"
 #include "DNA_linestyle_types.h"
 
 #include "BKE_context.h"
+#include "BKE_layer.h"
 #include "BKE_linestyle.h"
-#include "BKE_material.h"
 #include "BKE_modifier.h"
 #include "BKE_node.h"
 #include "BKE_paint.h"
 #include "BKE_particle.h"
 #include "BKE_scene.h"
+#include "BKE_workspace.h"
 #ifdef WITH_FREESTYLE
 #  include "BKE_freestyle.h"
 #endif
 
 #include "buttons_intern.h" // own include
 
-/****************** "Old Shading" Texture Context ****************/
-
-bool ED_texture_context_check_world(const bContext *C)
-{
-       Scene *scene = CTX_data_scene(C);
-       return (scene && scene->world);
-}
-
-bool ED_texture_context_check_material(const bContext *C)
-{
-       Object *ob = CTX_data_active_object(C);
-       return (ob && (ob->totcol != 0));
-}
-
-bool ED_texture_context_check_lamp(const bContext *C)
-{
-       Object *ob = CTX_data_active_object(C);
-       return (ob && (ob->type == OB_LAMP));
-}
-
-bool ED_texture_context_check_particles(const bContext *C)
-{
-       Object *ob = CTX_data_active_object(C);
-       return (ob && ob->particlesystem.first);
-}
-
-bool ED_texture_context_check_linestyle(const bContext *C)
-{
-#ifdef WITH_FREESTYLE
-       Scene *scene = CTX_data_scene(C);
-       SceneRenderLayer *actsrl;
-       FreestyleConfig *config;
-       FreestyleLineSet *lineset;
-       FreestyleLineStyle *linestyle;
-
-       if (scene && (scene->r.mode & R_EDGE_FRS)) {
-               actsrl = BLI_findlink(&scene->r.layers, scene->r.actlay);
-               config = &actsrl->freestyleConfig;
-               if (config->mode == FREESTYLE_CONTROL_EDITOR_MODE) {
-                       lineset = BKE_freestyle_lineset_get_active(config);
-                       if (lineset) {
-                               linestyle = lineset->linestyle;
-                               return linestyle && (linestyle->flag & LS_TEXTURE);
-                       }
-               }
-       }
-#else
-       (void)C;
-#endif
-       return false;
-}
-
-static void texture_context_check_modifier_foreach(void *userData, Object *UNUSED(ob), ModifierData *UNUSED(md),
-                                                   const char *UNUSED(propname))
-{
-       *((bool *)userData) = true;
-}
-
-bool ED_texture_context_check_others(const bContext *C)
-{
-       /* We cannot rely on sbuts->texuser here, as it is NULL when in "old" tex handling, non-OTHERS tex context. */
-       Object *ob = CTX_data_active_object(C);
-
-       /* object */
-       if (ob) {
-               /* Tex force field. */
-               if (ob->pd && ob->pd->forcefield == PFIELD_TEXTURE) {
-                       return true;
-               }
-
-               /* modifiers */
-               {
-                       bool check = false;
-                       modifiers_foreachTexLink(ob, texture_context_check_modifier_foreach, &check);
-                       if (check) {
-                               return true;
-                       }
-               }
-       }
-
-       /* brush */
-       if (BKE_paint_brush(BKE_paint_get_active_from_context(C))) {
-               return true;
-       }
-
-       return false;
-}
-
-static void set_texture_context(const bContext *C, SpaceButs *sbuts)
-{
-       Scene *scene = CTX_data_scene(C);
-
-       if (BKE_scene_use_new_shading_nodes(scene)) {
-               return;  /* No texture context in new shading mode */
-       }
-
-       {
-               bool valid_world = ED_texture_context_check_world(C);
-               bool valid_material = ED_texture_context_check_material(C);
-               bool valid_lamp = ED_texture_context_check_lamp(C);
-               bool valid_particles = ED_texture_context_check_particles(C);
-               bool valid_linestyle = ED_texture_context_check_linestyle(C);
-               bool valid_others = ED_texture_context_check_others(C);
-
-               /* this is similar to direct user action, no need to keep "better" ctxt in _prev */
-               if ((sbuts->mainb == BCONTEXT_WORLD) && valid_world) {
-                       sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_WORLD;
-               }
-               else if ((sbuts->mainb == BCONTEXT_MATERIAL) && valid_material) {
-                       sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_MATERIAL;
-               }
-               else if ((sbuts->mainb == BCONTEXT_DATA) && valid_lamp) {
-                       sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_LAMP;
-               }
-               else if ((sbuts->mainb == BCONTEXT_PARTICLE) && valid_particles) {
-                       sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_PARTICLES;
-               }
-               else if ((sbuts->mainb == BCONTEXT_RENDER_LAYER) && valid_linestyle) {
-                       sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_LINESTYLE;
-               }
-               else if ((ELEM(sbuts->mainb, BCONTEXT_MODIFIER, BCONTEXT_PHYSICS)) && valid_others) {
-                       sbuts->texture_context = sbuts->texture_context_prev = SB_TEXC_OTHER;
-               }
-               /* Else, try to revive a previous "better" ctxt... */
-               else if ((sbuts->texture_context_prev != sbuts->texture_context) &&
-                        (((sbuts->texture_context_prev == SB_TEXC_WORLD) && valid_world) ||
-                         ((sbuts->texture_context_prev == SB_TEXC_MATERIAL) && valid_material) ||
-                         ((sbuts->texture_context_prev == SB_TEXC_LAMP) && valid_lamp) ||
-                         ((sbuts->texture_context_prev == SB_TEXC_PARTICLES) && valid_particles) ||
-                         ((sbuts->texture_context_prev == SB_TEXC_LINESTYLE) && valid_linestyle) ||
-                         ((sbuts->texture_context_prev == SB_TEXC_OTHER) && valid_others)))
-               {
-                       sbuts->texture_context = sbuts->texture_context_prev;
-               }
-               /* Else, just be sure that current context is valid! */
-               else if (((sbuts->texture_context == SB_TEXC_WORLD) && !valid_world) ||
-                        ((sbuts->texture_context == SB_TEXC_MATERIAL) && !valid_material) ||
-                        ((sbuts->texture_context == SB_TEXC_LAMP) && !valid_lamp) ||
-                        ((sbuts->texture_context == SB_TEXC_PARTICLES) && !valid_particles) ||
-                        ((sbuts->texture_context == SB_TEXC_LINESTYLE) && !valid_linestyle) ||
-                        ((sbuts->texture_context == SB_TEXC_OTHER) && !valid_others))
-               {
-                       /* this is default fallback, do keep "better" ctxt in _prev */
-                       sbuts->texture_context_prev = sbuts->texture_context;
-                       if (valid_material) {
-                               sbuts->texture_context = SB_TEXC_MATERIAL;
-                       }
-                       else if (valid_lamp) {
-                               sbuts->texture_context = SB_TEXC_LAMP;
-                       }
-                       else if (valid_particles) {
-                               sbuts->texture_context = SB_TEXC_PARTICLES;
-                       }
-                       else if (valid_linestyle) {
-                               sbuts->texture_context = SB_TEXC_LINESTYLE;
-                       }
-                       else if (valid_world) {
-                               sbuts->texture_context = SB_TEXC_WORLD;
-                       }
-                       else if (valid_others) {
-                               sbuts->texture_context = SB_TEXC_OTHER;
-                       }
-               }
-       }
-}
-
 /************************* Texture User **************************/
 
 static void buttons_texture_user_property_add(ListBase *users, ID *id,
@@ -321,9 +153,6 @@ static void buttons_texture_users_from_context(ListBase *users, const bContext *
 {
        Scene *scene = NULL;
        Object *ob = NULL;
-       Material *ma = NULL;
-       Lamp *la = NULL;
-       World *wrld = NULL;
        FreestyleLineStyle *linestyle = NULL;
        Brush *brush = NULL;
        ID *pinid = sbuts->pinid;
@@ -335,42 +164,29 @@ static void buttons_texture_users_from_context(ListBase *users, const bContext *
                        scene = (Scene *)pinid;
                else if (GS(pinid->name) == ID_OB)
                        ob = (Object *)pinid;
-               else if (GS(pinid->name) == ID_LA)
-                       la = (Lamp *)pinid;
-               else if (GS(pinid->name) == ID_WO)
-                       wrld = (World *)pinid;
-               else if (GS(pinid->name) == ID_MA)
-                       ma = (Material *)pinid;
                else if (GS(pinid->name) == ID_BR)
                        brush = (Brush *)pinid;
                else if (GS(pinid->name) == ID_LS)
                        linestyle = (FreestyleLineStyle *)pinid;
        }
 
-       if (!scene)
+       if (!scene) {
                scene = CTX_data_scene(C);
+       }
+
+       const ID_Type id_type = pinid != NULL ? GS(pinid->name) : -1;
+       if (!pinid || id_type == ID_SCE) {
+               WorkSpace *workspace = CTX_wm_workspace(C);
+               ViewLayer *view_layer = BKE_workspace_view_layer_get(workspace, scene);
 
-       if (!pinid || GS(pinid->name) == ID_SCE) {
-               ob = (scene->basact) ? scene->basact->object : NULL;
-               wrld = scene->world;
                brush = BKE_paint_brush(BKE_paint_get_active_from_context(C));
-               linestyle = BKE_linestyle_active_from_scene(scene);
+               linestyle = BKE_linestyle_active_from_view_layer(view_layer);
+               ob = OBACT(view_layer);
        }
 
-       if (ob && ob->type == OB_LAMP && !la)
-               la = ob->data;
-       if (ob && !ma)
-               ma = give_current_material(ob, ob->actcol);
-
        /* fill users */
        BLI_listbase_clear(users);
 
-       if (ma && !limited_mode)
-               buttons_texture_users_find_nodetree(users, &ma->id, ma->nodetree, N_("Material"));
-       if (la && !limited_mode)
-               buttons_texture_users_find_nodetree(users, &la->id, la->nodetree, N_("Lamp"));
-       if (wrld && !limited_mode)
-               buttons_texture_users_find_nodetree(users, &wrld->id, wrld->nodetree, N_("World"));
        if (linestyle && !limited_mode)
                buttons_texture_users_find_nodetree(users, &linestyle->id, linestyle->nodetree, N_("Line Style"));
 
@@ -439,21 +255,8 @@ void buttons_texture_context_compute(const bContext *C, SpaceButs *sbuts)
        /* gather available texture users in context. runs on every draw of
         * properties editor, before the buttons are created. */
        ButsContextTexture *ct = sbuts->texuser;
-       Scene *scene = CTX_data_scene(C);
        ID *pinid = sbuts->pinid;
 
-       set_texture_context(C, sbuts);
-
-       if (!((sbuts->texture_context == SB_TEXC_OTHER) || BKE_scene_use_new_shading_nodes(scene))) {
-               if (ct) {
-                       BLI_freelistN(&ct->users);
-                       MEM_freeN(ct);
-                       sbuts->texuser = NULL;
-               }
-
-               return;
-       }
-
        if (!ct) {
                ct = MEM_callocN(sizeof(ButsContextTexture), "ButsContextTexture");
                sbuts->texuser = ct;
@@ -687,4 +490,3 @@ void uiTemplateTextureShow(uiLayout *layout, bContext *C, PointerRNA *ptr, Prope
                UI_but_func_set(but, template_texture_show, user->ptr.data, user->prop);
        }
 }
-