2016-09-13 54 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; 

顏色上面的代碼不檢查哪個鼠標按鈕已被使用,但我可以選擇一個對象,取消選擇對象,並選擇另一對象時它取消選擇當前對象。

我需要修改它以將取消選定的對象設置爲其原始顏色/材質,但應該相對簡單。

乾杯:)