2016-09-13 56 views
-1

我一直在使用GLSceneViewer1.Buffer.GetPickedObject(x,y)来选择GLViewceneMouseDown事件中的GLscene对象。我需要选择一个对象,改变颜色,用鼠标左键单击,取消选择另一个鼠标左键单击,如果选择了另一个对象,则取消选择它。看来,TGLSceneObject需要一个属性IsPicked:布尔值为我能够实现这一点。如果有人知道不用修改GLScene这样做会很酷。这是我写的那种作品的代码,但有些没有。 SetSelected(Selected,SelectedColor)仅更改所选对象的颜色。GLScene采摘

procedure TForm32.GLSceneViewer1MouseDown(Sender : TObject; Button : TMouseButton; Shift : TShiftState; X, Y : Integer); 
    var 
    Selected : TGLSceneObject; 
    AButton : TGLMouseButton; 
    begin 
    AButton := TGLMouseButton(Button); 

    // if an object is picked... 
    Selected := (GLSceneViewer1.Buffer.GetPickedObject(x, y) as TGLSceneObject); 

     case AButton of 
     mbLeft: 
      begin 
       if(Selected <> UnSelected) then 
       begin 
        if(Assigned(Selected)) then 
         begin 
         SetSelected(Selected, SelectedColor); 
         StatusBar1.Panels[0].Text := 'Selected'; 
         UnSelected := Selected; 
         end 
        else 
        if(not Assigned(Selected)) then 
         begin 
         UnSelected.Material.FrontProperties.Emission.Color:= clrBlack; 
         UnSelected.Material.FrontProperties.Ambient.Color := clrGray20; 
         UnSelected.Material.FrontProperties.Diffuse.Color := clrGray80; 
         StatusBar1.Panels[0].Text := 'Unselected'; 
         UnSelected := Selected; 
         end; 
       end; 
      end; 
     end; 
    end; 

对我来说这将是更容易:

procedure TForm32.GLSceneViewer1MouseDown(Sender : TObject; Button : TMouseButton; Shift : TShiftState; X, Y : Integer); 
    var 
    Selected : TGLSceneObject; 
    begin 
    Selected := (GLSceneViewer1.Buffer.GetPickedObject(x, y) as TGLSceneObject); 
     if(not Selected.IsPicked) then 
     SetSelected(Selected, SelectedColor) 
     else 
     SetSelected(Selected, UnSelectedColor); 
    end; 

回答

0

我是否应该通过修改源代码,这将需要有分发源代码,这没”打破我的GLScene库的一些辩论后对我来说似乎很理想,我已经拿出下面的代码作为我的问题的答案。看起来的答案是维护一个我命名为CurrentSelection的TGLSceneObject缓冲区对象。

procedure TForm32.GLSceneViewer1MouseDown(Sender : TObject; Button : TMouseButton; Shift : TShiftState; X, Y : Integer); 
    //CurrentSelection : TGLSceneObject; //TForm private declaration 
    //Selected : TGLSceneObject; //TForm private declaration 
    begin 
    Selected := (GLSceneViewer1.Buffer.GetPickedObject(x, y) as TGLSceneObject); 
     if(Selected <> nil) then //Avoid access violation error 
     begin 
      if(Assigned(CurrentSelection)) then //If CurrentSelection is not nil deselect it 
      SetSelectedColor(CurrentSelection, UnSelectedColor); 

      if(Selected = CurrentSelection) then 
      begin 
       //has the same object been clicked then deselect it and clear the buffer object 
       SetSelectedColor(Selected, UnSelectedColor); 
       CurrentSelection := nil; 
      end 
      else 
      begin 
       //if no object is selected select an object, set the color and assign the object to the buffer 
       SetSelectedColor(Selected, SelectedColor); 
       CurrentSelection := Selected; 
      end; 
     end; 
    end; 

设置的TGLSceneObject

procedure TForm32.SetSelectedColor(Selected : TGLSceneObject; Color : TColorVector); 
    begin 
    Selected.Material.FrontProperties.Ambient.Color := Color; 
    Selected.Material.FrontProperties.Diffuse.Color := Color; 
    Selected.Material.FrontProperties.Emission.Color:= clrBlack; 
    end; 

颜色上面的代码不检查哪个鼠标按钮已被使用,但我可以选择一个对象,取消选择对象,并选择另一对象时它取消选择当前对象。

我需要修改它以将取消选定的对象设置为其原始颜色/材质,但应该相对简单。

干杯:)